Javascript 主干视图未填充
我正在将集合和模型传递给我的视图:Javascript 主干视图未填充,javascript,backbone.js,Javascript,Backbone.js,我正在将集合和模型传递给我的视图: popModel = new GenreModel({genre_name:'Pop',genre_id:'pop'}); popView = new GenreView ({model:popModel,collection:new PopCol()}); 这是视图的实现: Backbone.View.extend ({ className:'sect', initialize: function () {
popModel = new GenreModel({genre_name:'Pop',genre_id:'pop'});
popView = new GenreView ({model:popModel,collection:new PopCol()});
这是视图的实现:
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
你好
视图未填充,但当我调用console.log(popView.render.el)时,我得到:
hello
<div class="sect">…</div>
hello
你好
…
你好
然后视图将正确呈现。我如何修复此问题?我猜您正在尝试在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;
},
我猜你是想在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;
},
你能给我们一把小提琴吗?你能给我们一把小提琴吗?