好的, super 基本 Backbone 问题-我一直在寻找这个问题,但是尽管有很多类似的问题,但是太慢了。放心,我很suitably愧。
无论如何,足够的自我鞭--为什么不渲染?
var app = app || {};
app.Option = Backbone.Model.extend({
url: 'http://localhost:4711/api'
//This url contains the following JSON: {"title": "Blahblah", "author": "Luke Skywalker"};
});
app.View = Backbone.View.extend({
el: 'body',
initialize: function(){
this.model.fetch();
this.model.bind('change', this.render(), this);
},
render: function(){
this.$el.html(this.model.get('title'));
return this;
}
});
$(function() {
var option = new app.Option();
this.homeView = new app.View({ //Tried changing this to a standard var declaration but didn't work
model: option
});
this.homeView.render();
});
因此,我期望在屏幕上看到JSON“Blahblah”,但什么也看不到。正确获取了JSON(我可以在Firebug控制台中看到成功的GET请求),并且我认为我已经确保在尝试呈现数据之前已获取了数据...
那怎么了控制台给我这个错误:“TypeError :(中间值).callback.call不是函数”
谢谢!
最佳答案
一件事是您在事件绑定(bind)中立即调用this.render()
,而不仅仅是绑定(bind)回调。改为这样做(使用listenTo
以获得最佳做法):
initialize: function(){
this.listenTo(this.model, 'change', this.render);
this.model.fetch();
}
模型是否可能实际上没有更改?您可以尝试绑定(bind)到
sync
而不是change
,以查看是否可行。您还将渲染两次。一次直接使用
this.homeView.render()
,一次通过事件处理程序。如果您确实希望将模型保存在initialize
中并绑定(bind)到change事件,则不需要直接渲染。玩那些,看看是否能解决问题。