我正在尝试 Backbone.js 应该是一项基本任务,但无法让它工作。我只想对我的模型执行获取操作并让它更新 View 。这是我的代码:
(function($) {
var HomeModel = Backbone.Model.extend({
defaults: {
lead: "not logged in",
},
url: 'test.php'
});
var HomeView = Backbone.View.extend({
initialize: function() {
this.model = new HomeModel();
this.model.bind("change", this.render);
this.model.fetch();
},
el: $("#content"),
render: function() {
this.$el.html(this.model.get("lead"));
}
});
var homeView = new HomeView();
})(jQuery);
我可以看到“test.php”加载成功,但 render 函数从未被调用。
我错过了什么?
最佳答案
您是如何看到“test.php”加载成功的?也许 AJAX 请求成功,但响应不是有效的 JSON?尝试向 fetch 调用添加成功和错误回调:this.model.fetch({success: function (resp) { console.log('success: %o', resp) }, error: function (er) { console.log('error: %o', er) });
关于javascript - 使 backbone.js 'fetch' 自动更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10791546/