그래서 여기 jsfiddle에 내 응용 프로그램의 예입니다 http://jsfiddle.net/GWXpn/1/백본 발사되지 않음 이벤트
문제는 이벤트가 전혀 해고하지 않을 클릭합니다. 콘솔에 JS 오류가 표시되지 않습니다.
먼저 항목이 있으면 각 항목을 클릭 할 수있는 순서가 아닌 목록을 표시하고 싶습니다.
var FooModel = Backbone.Model.extend({});
var ListView = Backbone.View.extend({
tagName: 'ul', // name of (orphan) root tag in this.el
initialize: function() {
_.bindAll(this, 'render'); // every function that uses 'this' as the current object should be in here
},
render: function() {
for (var i = 0; i < 5; i++) {
var view = new SingleView({
model: new FooModel()
});
$(this.el).append(view.render().el);
}
return this; // for chainable calls, like .render().el
}
});
var SingleView = Backbone.View.extend({
tagName: 'li', // name of (orphan) root tag in this.el
initialize: function() {
_.bindAll(this, 'render', 'click'); // every function that uses 'this' as the current object should be in here
},
events: {
"click": "click"
},
click: function(ev) {
console.log("aaa");
alert(333);
},
render: function() {
$(this.el).append("aaa");
return this; // for chainable calls, like .render().el
}
});
나는 여러 모듈 (머리글, 본문, 바닥 글)에 내 응용 프로그램을 분할하고 싶었다 그래서 추상적 인 모델을 생성하고 그것에서 내 모듈을 확장 : 이것은 내가 무슨 짓을
var AbstractModule = Backbone.Model.extend({
getContent: function() {
return "TODO";
},
render: function() {
return $('<div></div>').append(this.getContent());
}
});
var HeaderModule = AbstractModule.extend({
id: "header-module",
});
var BodyModule = AbstractModule.extend({
id: "body-module",
getContent: function() {
var listView = new ListView();
return $("<div/>").append($(listView.render().el).clone()).html();
}
});
var ModuleCollection = Backbone.Collection.extend({
model: AbstractModule,
});
그런 다음 주뷰를 작성하여 모든 하위보기를 렌더링했습니다.
var AppView = Backbone.View.extend({
el: $('#hello'),
initialize: function (modules) {
this.moduleCollection = new ModuleCollection();
for (var i = 0; i < modules.length; i++) {
this.moduleCollection.add(new modules[i]);
}
},
render: function() {
var self = this;
_(this.moduleCollection.models).each(function (module) { // in case collection is not empty
$(self.el).append(module.render());
}, this);
}
});
var appView = new AppView([HeaderModule, BodyModule]);
appView.render();
이유가 무엇입니까? 명시 적으로 요청하지 않는 한 이벤트를 복사하지 않습니다 모든
return $("<div/>").append($(listView.render().el).clone()).html();
첫째, clone
:
클릭하면 경고가 발생합니다. 또한, 귀하의 바이올린은 CSS 창에서 자바 스크립트가 있습니다. – Prisoner
@ 죄수 죄송합니다. 잘못된 jsfiddle 링크를 게시했습니다. 나는 그것을 지금 고쳤다. ... –
이것은 백본의 버전과 관련된 문제인 것 같다 (0.3.3 심각하게?). 나는 누군가가 너를 도울 수 있을지 의심 스럽다. – Loamhoof