我有以下backbone.js脚本。传统上我使用以下方式渲染视图:
var myItem = new Item();
myItem.fetch(); var reward_view = new ListView({model:myItem}); reward_view.render();
但是,我见过的大多数examples都没有这样做,并且以某种方式允许您使用以下方式加载/初始化视图:
var listView = new ListView();
怎么样?这就是我的尝试:
(function($){
var Item = Backbone.Model.extend({
urlRoot: '/api/v1/1/'
});
var List = Backbone.Collection.extend({
model: Item
});
var ListView = Backbone.View.extend({
el: '#reward_view',
initialize: function(){
_.bindAll(this, 'render'); // every function that uses 'this' as the current object should be in here
this.collection = new List();
this.render();
},
render: function(){
console.log(this)
$(this.el).html('<h1>Hello' + this.model.get('name') + '</h1>');
return this; // for chainable calls, like .render().el
}
});