Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干模型获取刷新事件未触发_Javascript_Backbone.js - Fatal编程技术网

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

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:

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

型号

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,我在文档中完全忽略了这一点。但事实证明,我还需要在文档模型的解析方法中设置对模型的响应。谢谢你的帮助。