1
全码... http://pastebin.com/EEnm8vi3
线378未插入的部分到当前视图。 节模型正确传入方法。其他一切按预期工作,除了插入子呈现的视图。
我想知道为什么$(this.el)是空的,因此不允许追加。试图使用像$('#sections')这样的直接选择器也不起作用。
培训相关的代码从上面pastbin链接重复:(方法AddOne)
SCC.Views.SectionView = Backbone.View.extend({
tagName: "div",
className: "section",
initialize: function(){
_.bindAll(this, 'render');
this.template = _.template($('#section-tmpl').html());
},
render: function() {
console.log($(this.el));
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});
SCC.Views.SectionsView = Backbone.View.extend({
tagName: "div",
id: "sections",
className: "sections",
initialize: function(){
_.bindAll(this, 'render');
//SCC.Sections.bind('add', this.addOne, this);
SCC.Sections.bind('reset', this.addAll, this);
SCC.Sections.bind('all', this.render, this);
},
render: function() {
$(this.el).html("<p>rendered</p>");
return this;
},
addOne: function(section) {
var view = new SCC.Views.SectionView({model: section});
$(this.el).append(view.render().el);
},
addAll: function() {
this.collection.each(this.addOne);
}
});
SCC.Sections = new SCC.Collections.Sections();
SCC.SectionsView = new SCC.Views.SectionsView({collection:SCC.Sections});
SCC.Sections.reset(window.SectionData);
$('#main').append(SCC.SectionsView.render().el);
尝试将addOne和addAll添加到_.bindAll(this,...)函数中。我想为什么它可能会失败,因为addAll的“this”不是BackboneView,除非你已经绑定了它。 – Bartek
我认为这样做!!!!!也摆脱了'$(this.el).html(“
呈现
”); '我认为有帮助? – lukemh而不是写'$(this.el)',这意味着你多次将元素转换为jQuery,你应该使用'this。$ el'。为了方便和高效,它被内置到Backbone中。 – Micros