Javascript 主干模型获取刷新事件未触发
我希望我能抽象出所有相关的部分。我的问题是,当我从服务器获取模型时,为什么render方法没有执行 型号Javascript 主干模型获取刷新事件未触发,javascript,backbone.js,Javascript,Backbone.js,我希望我能抽象出所有相关的部分。我的问题是,当我从服务器获取模型时,为什么render方法没有执行 型号 var Document = Backbone.Model.extend({ urlRoot: "/dms/reconcile/GetDocumentByDocumentId/" }); 查看 window.DocumentView = Backbone.View.extend({ el: $("#reconcile_document"), initialize:
var Document = Backbone.Model.extend({
urlRoot: "/dms/reconcile/GetDocumentByDocumentId/"
});
查看
window.DocumentView = Backbone.View.extend({
el: $("#reconcile_document"),
initialize: function () {
this.model.bind('refresh', this.render) //also tried reset
},
render: function () {
alert("Do awesome stuff here");
return this;
}
});
路线
var AppRouter = Backbone.Router.extend({
routes: {
"package/:id": "getPackage"
},
getPackage: function (packageid, p) {
window._document = new Document({ id:packageid) });
window.document = new DocumentView({ model: _document });
_document.fetch();
}
});
// Instantiate the router
var app_router = new AppRouter;
因此,当我转到localhost:3000/#/Package/123
时,我可以看到对localhost:3000/dms/reconcile/GetDocumentByDocumentId/123的xhr调用成功地返回了数据,但render
函数从未执行。任何帮助都将不胜感激
干杯。对模型调用fetch()
不会触发刷新事件。在获取数据后,它将通过调用set
触发change
事件:
及
更改事件处理程序,它应该可以工作:
window.DocumentView = Backbone.View.extend({
el: $("#reconcile_document"),
initialize: function () {
this.model.bind('change', this.render, this);
},
render: function () {
alert("Do awesome stuff here");
return this;
}
});
或者,您可以调用_document.fetch({success:_document.success\u handler})。在success_handler中,您可以在您的模型上触发自定义事件。我很感激Derick,我在文档中完全忽略了这一点。但事实证明,我还需要在文档模型的解析方法中设置对模型的响应。谢谢你的帮助。