Using a backbone model, assuming it fetches some additional properties from the server (like "FirstName" and "LastName"), I have something like the following:
var myModel = new Backbone.Model({ id: 10 });
var myOtherModel = new Backbone.Model({ id: 20 });
$.when(myModel.fetch(), myOtherModel.fetch()).done(function () {
console.log(myModel.toJSON());
});
The output:
{ id: 10 }
The output a moment later:
{ id: 10, FirstName: "Joe", LastName: "Schmo" }
It would seem that Backbone's promises are flawed in that the jqXHR object returned has a promise that's resolved prior to backbone completing its own process.
Is this the case? Is there something else that must be done to ensure that the promise returned by fetch()
isn't resolved until all of Backbone's set
s are done, or at least the set
s happen before my attached handler?
Found the issue.
My model that I was fetching overrode Backbone.Model.sync.
sync: function (method, model, options) {
// Some stuff
Backbone.sync(method, model, options); // missing return
}
Because it didn't return Backbone.sync()
the promise was resolving immediately. I would have caught this if not for the $.when()
wrapping the fetch()
calls, which silently will accept any old thing, even if it's not a promise, and simply treat it as resolved immediately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With