In my application I override Backbone.sync as follows:
Backbone.sync = function(method, model, options){ 
    //Some custom code
    //THIS FAILS.
    Backbone.prototype.sync.call(this, method, model, options); 
}}
My question is, how do I call the original sync method? Do I need to use this.sync instead?
Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.
Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).
From what I understand, Backbone.sync checks to see if there is a locally defined version of sync and calls that before calling the global Backbone.sync :
(this.sync || Backbone.sync)
So, given that your Model is something like TestModel. I think you can do something like this (forgive, me this might not be the correct syntax, javascript is far from my specialty):
var TestModel = Backbone.Model.extend({ 
    "sync": function(method, model, options) { 
        //Some custom code
        Backbone.sync(method, model, options); 
    }
});
This is what I've gathered from here and here
Try something like this, might not be the best solutions but it works:
var parentSyncMethod = Backbone.sync; //save parent method, the override
Backbone.sync = function() {
    // Your code here.
    var parentSyncMethod.apply(Backbone, arguments);
};
Hope it helps in some way
var TestModel = Backbone.Model.extend({ 
    sync: function(method, model, options){  
        // some code here
        return Backbone.sync(method, model, options); 
    }
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With