How do you override the synchronization method for the Backbone collection (as opposed to the model)?

I have a folder tree that retrieves a collection based on the folder you selected. If the user changes his mind and clicks on another folder, I want to abort the previous selection. Since this happens in several places within the project, I want to override the synchronization method in the collection. I have seen so many examples of model synchronizations, but not for collections. I also want to save query string parameters. The official documentation states that collections also have a synchronization method, but I have never seen this. Please point me in the right direction. Thank you in advance.

+4
source share
1 answer

Synchronization with the database looks exactly like the model synchronization method:

// Proxy `Backbone.sync` by default. sync: function() { return Backbone.sync.apply(this, arguments); }, 

thats, because both do the same thing, they are just the "proxy" method of the Backbone.sync. The reason they exist is to allow implementations to change the synchronization logic based on each type and not touch the main synchronization method, which will affect all models and collections in your project.

I would advise doing something like the following for your collection, because you probably do not want to simulate the baseline synchronization logic yourself, it does a few things for you, and messing with it can cause problems that can be difficult to solve later.

 var MyCollectionType = Backbone.Collection.extend({ sync: function(method, model, options){ //put your pre-sync logic here and call return; if you want to abort Backbone.Collection.prototype.sync.apply(this, arguments); //continue using backbone collection sync } }); 
+1
source

Source: https://habr.com/ru/post/1439919/


All Articles