我正在将一个集合和一个模型传递给我的 View :
popModel = new GenreModel({genre_name:'Pop',genre_id:'pop'});
popView = new GenreView ({model:popModel,collection:new PopCol()});
这是 View 的实现:
Backbone.View.extend ({
className:'sect',
initialize: function ()
{
context = this;
this.collection.fetch ({
success:function ()
{
context.render();
}
});
},
render: function ()
{
console.log("hello");
this.$el.html (_.template(view,this.model.toJSON()));
this.collection.each (function (video)
{
genreLi = new GenreLi ({model:video});
this.$(this.model.genre_id).append (genreLi.el);
},this);
return this;
},
});
我的问题是,当我调用 console.log (popView.el) 时,我收到以下信息:
<div class="sect"></div>
hello
View 没有填充,但是当我调用 console.log (popView.render.el) 时,我得到:
hello
<div class="sect">…</div>
hello
View 然后正确呈现。我该如何解决这个问题?
最佳答案
我猜您正在尝试在 ul li 列表中显示相同类型的所有视频。 我认为您在获取元素时缺少 id 之前的“#”。
render: function ()
{
...
this.collection.each (function (video)
{
genreLi = new GenreLi ({model:video});
// if you have already defined elements by genre id and not within the view's el
// This is not good as a view should attach new html into dom elements under it
$('#'+this.model.genre_id).append (genreLi.el);
// if you have defined elements by genre id within in the view'w el then
this.$el.find('#'+this.model.genre_id).append (genreLi.el);
},this);
return this;
},
关于javascript - 主干 View 未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15262787/