gpt4 book ai didi

javascript - Backbone 模型获取刷新事件未触发

转载 作者:行者123 更新时间:2023-12-02 07:09:07 25 4
gpt4 key购买 nike

我希望我能抽象出所有相关部分。我的问题是为什么当我从服务器获取我的模型时渲染方法没有执行。

型号

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() 不会触发 refresh 事件。它会在获取数据后调用 set 来触发 change 事件:

http://documentcloud.github.com/backbone/docs/backbone.html#section-40

http://documentcloud.github.com/backbone/#Model-fetch

更改您的事件处理程序,它应该可以工作:

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;
}
});

关于javascript - Backbone 模型获取刷新事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com