admin管理员组

文章数量:1352266

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 es back successfully but the render function never executes. Any help would be greatly appreciated.

Cheers.

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 es back successfully but the render function never executes. Any help would be greatly appreciated.

Cheers.

Share Improve this question asked Oct 21, 2011 at 23:56 Amin EshaqAmin Eshaq 4,0241 gold badge19 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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./backbone/docs/backbone.html#section-40

and

http://documentcloud.github./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;
    }
});

本文标签: javascriptBackbone Model Fetch refresh event not firingStack Overflow