1

Without having to implement a custom collection.sync, is it possible to send custom headers to the server when Backbone.js calls its sync method?

That is, I want to pass a customer header to the server when I perform an operation, like collection.fetch.

Thank you.

Swisher Sweet
  • 769
  • 11
  • 33

1 Answers1

1

You can easily extend the .sync method without having to re-write it

myApp.originalSync = Backbone.sync;
Backbone.sync = function(method, model, options) {
    options || (options = {});
    options.headers = { "your": "custom header" };
    return myApp.originalSync(method,model,options);
};
Stephen Thomas
  • 13,843
  • 2
  • 32
  • 53
  • Thanks. If I don't want the header to be used when syncing all collections/models, can I just spend specific headers for certain collections as an example? – Swisher Sweet Mar 13 '14 at 19:30
  • Just override the `.sync` method of the appropriate collection(s). Instead of `Backbone.sync` in the snippet above, use `MyCollection.sync`. – Stephen Thomas Mar 13 '14 at 19:42