Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Backbone.sync but save its previous functionality

I'd like to override Backbone.sync but also have the original Backbone.sync functionality run after my additions. I guess kind of like calling super on a superclass in Java. Is there a way to do this other than copying all the previous code over?

like image 754
kreek Avatar asked Oct 14 '11 15:10

kreek


1 Answers

In JavaScript, you can store any property or method in a variable. The following example will assign Backbone.sync to another variable and then at the end of your function call it with all the variables that are passed to your new Backbone.sync function.

var originalSync = Backbone.sync;
Backbone.sync = function() {
    // Your code here.
    return originalSync.apply(Backbone, arguments);
};
like image 129
Brian Nickel Avatar answered Nov 15 '22 16:11

Brian Nickel