主干模型获取刷新事件未触发

发布于 2024-12-11 10:23:31 字数 1260 浏览 0 评论 0原文

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

模型

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-12-18 10:23:31

在模型上调用 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;
    }
});

Calling fetch() on a model won't fire a refresh event. It will fire a change event by calling set 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:

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;
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文