I was checking the code from a Backbone JQuery mobile sample app http://jquerymobile.com/test/docs/pages/backbone-require.html
and found the following inside the collection object
// Overriding the Backbone.sync method (the Backbone.fetch method calls the sync method when trying to fetch data)
sync: function( method, model, options ) {
// Local Variables
// ===============
// Instantiates an empty array
var categories = [],
// Stores the this context in the self variable
self = this,
// Creates a jQuery Deferred Object
deferred = $.Deferred();
// Uses a setTimeout to mimic a real world application that retrieves data asynchronously
setTimeout( function() {
// Filters the above sample JSON data to return an array of only the correct category type
categories = _.filter( self.jsonArray, function( row ) {
return row.category === self.type;
} );
// Calls the options.success method and passes an array of objects (Internally saves these objects as models to the current collection)
options.success( categories );
// Triggers the custom `added` method (which the Category View listens for)
self.trigger( "added" );
// Resolves the deferred object (this triggers the changePage method inside of the Category Router)
deferred.resolve();
}, 1000);
// Returns the deferred object
return deferred;
}
I just want to understand the part that deferred is declared and returned, why do we even need that? No callback is being attached to it. And also I really don't understand why we use setTimeout and have the deferred object resolve inside it.