我只是在学习backbone.js。我写了下面的例子,但它不起作用,为什么?
它应该只在我的div元素中显示名称。
(function($) {
var Item = Backbone.Model.extend({
urlRoot: '/api/camp/1/'
});
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
},
render: function() {
console.log(this)
$(this.el).html('<h1>Hello' + this.model.get('name') + '</h1>');
return this; // for chainable calls, like .render().el
}
});
var reward_view = new ListView();
})(jQuery);
答案 0 :(得分:3)
您错过了模型的实例化,然后您必须获取该模型并将其传递给您的视图,之后您必须调用视图的渲染。
(function($) {
var Item = Backbone.Model.extend({
urlRoot: '/api/camp/1/'
});
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
},
render: function() {
console.log(this)
$(this.el).html('<h1>Hello' + this.model.get('name') + '</h1>');
return this; // for chainable calls, like .render().el
}
});
var myItem = new Item();
myItem.fetch();
var reward_view = new ListView({model:myItem});
reward_view.render();
})(jQuery);