我正在从骨干网中的休息 url 获取模型对象的集合。然后我遍历集合并使用下划线模板帮助器方法处理来自模型的数据。但是在调用该属性时我得到了未定义的 defaultImg。
看法:
define (['jquery','underscore','backbone'],function($,_,Backbone){
PopVideoView = Backbone.View.extend ({
tagName:"li",
//template: _.template($('#popView').html()),
template: _.template("<li><img src='<%= defaultImg %>'/></li>"),
render: function ()
{
console.log(this.model.toJSON());
this.template(this.model.toJSON());
this.$el.html(this.template);
return this;
}
});
return PopVideoView;
});
另一种观点:
define(['jquery','underscore','backbone','collections/PopVideos','views/PopVideo'],function($,_,Backbone){
PopVideosView = Backbone.View.extend ({
el:"#pop",
render: function ()
{
this.collection.each (function(video)
{
popVideoView = new PopVideoView({model:video});
this.$el.append (popVideoView.render().el).hide().fadeIn(300);
},this);
return this;
},
});
return PopVideosView;
});
这是我从 Chrome 开发者控制台得到的:
Object {video_id: "1G4isv_Fylg", video_name: "Coldplay - Paradise ", defaultImg: "http://i.ytimg.com/vi/1G4isv_Fylg/mqdefault.jpg", genre: "pop", date: "Feb 16, 2013 1:01:33 PM"…}
Uncaught ReferenceError: defaultImg is not defined
我做错了什么?
这是模型和集合:
define (['jquery','underscore','backbone'],function($,_,Backbone){
Video = Backbone.Model.extend ({
urlRoot:"/video",
});
return Video;
});//end define
define(['backbone','models/Video'],function(Backbone,Video) {
PopVideosCollection = Backbone.Collection.extend ({
model:Video,
url:"/pop/3/1"
});
return PopVideosCollection;
});