Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to code inorder to override backbone.sync

I want to override backbone.sync i have already asked this but the problem is i don't quite get it. I need to know where to put the codes if i were to override the sync function.

If i put it on the model like this

model = Backbone.Model.extend({ sync:"" });

Then how should i call it? if i were to use the save method. Also i need to change the methodMap of create from POST to PUT. temporarily i resorted to this 'create': 'PUT', actually editing the backbone.js file ( iknow its not good ). Before i forgot i also need to add this

sendAuthentication = function (xhr) {
          xhr.setRequestHeader('Authorization', auth)
}; 

As a beforeSend parameter since my server has authentication. Again where should i do it? Where should i go and put the codes? in my model? in my collection? or in my views? Any help? THank you.

update

Also can i override the sync on my collection? i mean can i have something like this?

collection = Backbone.Collection.extend({ sync:""});
like image 664
n0minal Avatar asked Apr 10 '12 05:04

n0minal


1 Answers

The strategy behind Backbone framework is to make it simple for editing and flexible for every need. So if you look up the source code you'll find out that every method, which calls Backbone.sync in fact calls first "this.sync".

From the Backbone manual you can read :

The sync function may be overriden globally as Backbone.sync, or at a finer-grained level, by adding a sync function to a Backbone collection or to an individual model.

So you have two options

Option One - Replacing global Backbone.sync function

If you override the global Backbone.sync you should place your code in your global application file ( actually anywhere you want, but it must be evaluated ( executed ) at your initial javascript loading, to work as expected

// Anywhere you want

Backbone.sync = function(method, collection, options) {
        console.log(method, collection options)
}

This will override Backbone.sync and actually will display on your console what is called every time you call collection.fetch, save, delete, etc.

Here you have no default Methodmap, infact you have nothing else except the arguments :

  • method - which is a string - 'read', 'create', 'delete', 'update'
  • collection - which is your collection instance which calls the method
  • options - which has some success, error functions, which you may or may not preserve.

Debug this in your browser, while reading the Backbone source code, it's very easy to understand.

Option Two - Adding to your model/collection sync method

This is used if you wish 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 won't call Backbone.sync anymore, but yor Log.fetch() method will.

like image 171
drinchev Avatar answered Sep 19 '22 01:09

drinchev