I\'m looking for a better solution for two things:
How can I understand if the data is fetched and ready, I use BasicDealList.on(\"reset\", function()
I know this question has already been answered but here is an alternative.
BasicDealCollection = Backbone.Collection.extend({
model: BasicDeal,
url: '/some/ajax/url/',
});
myCollection = new BasicDealCollection()
deferred = myCollection.fetch()
$.when(deferred).then(function() {
// do stuff when we have the data.
});
The key benefit of this is that we are using the "when" function. The "when" function gives us the ability to check multiple fetch calls and do one success.
$.when(deferredOne, deferredTwo, deferredThree).then(function() {
// do stuff when we have the data.
});
Also, if we stored the deferred object into a variable, we can do things like this. The variable will be a flag to let us know that we already loaded the data.
if (deferred.state() === "resolved") {
// do stuff when we have the data.
}
When we call fetch() on a collection it returns a jQuery deferred object. A jQuery deferred object can be in 3 states, "pending", "rejected" or "resolved" and once we have the data, it will set the state of the deferred object to resolved.