Backbone Framework's strategy is to make it easy to edit and flexible for every need. Therefore, if you look at the source code, you will find that every method that calls Backbone.sync actually calls "this.sync" first.
In the base book reference you can read:
The synchronization function can be redefined globally as Backbone.sync or a more subtle level by adding a synchronization function to the baseline of the collection or individual model.
So you have two options
Option One - Replacing the global Backbone.sync function
If you redefine the global Backbone.sync, you must put your code in your global application file (in fact, anywhere you want, but you need to evaluate (execute) it during the javascript bootup to work as expected
This will override Backbone.sync and actually display on the console what is called every time you call collection.fetch, save, delete, etc.
Here you have no default Methodmap, infact you have nothing else but arguments:
- is the line - 'read', 'create', 'delete', 'update'
- collection is your collection instance that calls the method
- which have some successes, error functions that you may or may not save.
Debug this in your browser by reading the source code of Backbone, it is very easy to understand.
Option two - add a model / collection to the synchronization method
This is used if you want to use the default Backbone.sync method for every other model / collection, except the one you specifically define:
mySocketModel = Backbone.Model.extend({ sync : function(method, collection, options) { console.log('socket collection '+this.name+' sync called'); } }); Partners = new mySocketModel({ name : 'partners' }); Users = new mySocketModel({ name : 'users' }); Log = new Backbone.Collection;
So, if you call Partners.fetch () or Users.fetch (), they will no longer call Backbone.sync, but the yor Log.fetch () method will be.
drinchev
source share