渲染子视图时,主干视图中不存在$(this.el)
no existant $(this.el) in backbone view while rendering child view
完整代码在这里…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);
我自己也遇到过这个问题,所以我把这个答案留给其他人:
当你绑定这个。渲染到"all",就像@lukemh做的那样:
SCC.Sections.bind('all', this.render, this);
你实际上是在说每次在模型/集合中触发事件时重新渲染视图。当你在render方法中使用.html()时,你还将通过addOne函数覆盖任何可能已经.append() ed到它的子视图。
如果你移动$(this.el).html()调用到初始化视图,问题就解决了。你仍然可以将render绑定到'all',但要确保你只重新渲染其中的一部分,否则你会再次覆盖子视图。
相关文章:
- 正在将事件处理程序添加到不存在的类
- javascript如果图像不存在don't加载它
- 由于响应中不存在“Access Control Allow Origin”标头,跨域请求停止工作
- 如何在ui路由器angularjs中只更改子ui视图,同时保持父视图不变
- JavaScript-如果以前不存在文本,如何从文本区域删除新行
- 如何将一个函数附加到一个不存在的元素上
- 在scala或scalajs Diode中,现有类型中的任何一种都符合“;更新一个没有'还不存在”;
- 查找数组's按属性不存在于另一个数组中的对象
- 带有粘性标题的角度ng视图不起作用
- 用javascript在对象上创建不存在的方法
- 仅当值不为'不存在
- Angular JS-视图不工作
- casperjs-css选择器存在,但当单击它时会引发“”;CasperError:无法在不存在的选择器“”上调度mou
- 运行时不存在Javascript函数
- 使用webdriver和selenium验证元素是否不存在
- 如果文件不存在,fs.watch是否有错误处理程序
- 试图把注意力集中在一个不重要的元素上是不是一种糟糕的做法;不存在
- Backbonejs:在视图中渲染的模型,但在集合中不存在
- 渲染子视图时,主干视图中不存在$(this.el)
- Yii2-渲染时视图文件不存在('profile#parentVerticalTab3')