主干模型获取刷新事件未触发
我希望我能抽象出所有相关部分。我的问题是为什么当我从服务器获取模型时渲染方法没有执行。
模型
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
时,我可以看到xhr 调用 localhost:3000/dms/reconcile/GetDocumentByDocumentId/123
并且数据成功返回,但 render
函数永远不会执行。任何帮助将不胜感激。
干杯。
I hope i can abstract all the relevant parts. My question is why the render method isn't executing when i fetch my model from the server.
Model
var Document = Backbone.Model.extend({
urlRoot: "/dms/reconcile/GetDocumentByDocumentId/"
});
View
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;
}
});
Route
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;
So when i go to localhost:3000/#/Package/123
I can see that the xhr call to localhost:3000/dms/reconcile/GetDocumentByDocumentId/123
and the data comes back successfully but the render
function never executes. Any help would be greatly appreciated.
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在模型上调用
fetch()
不会触发refresh
事件。它将在获取数据后通过调用set
来触发change
事件:http://documentcloud.github.com/backbone/docs/backbone.html#section-40
和
http://documentcloud.github.com/backbone/#Model-fetch
更改您的事件处理程序它应该起作用:
Calling
fetch()
on a model won't fire arefresh
event. It will fire achange
event by callingset
after it fetches the data:http://documentcloud.github.com/backbone/docs/backbone.html#section-40
and
http://documentcloud.github.com/backbone/#Model-fetch
Change your event handler and it should work: