最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Where to code inorder to override backbone.sync - Stack Overflow

programmeradmin2浏览0评论

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:""});

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:""});
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 10, 2012 at 5:32 n0minaln0minal 3,2239 gold badges48 silver badges72 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

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.

发布评论

评论列表(0)

  1. 暂无评论