0

This would never be for production, but for development only. What I would like to do is override what I think is the sync method (when you do model.save, model.update and model.destroy) to then pass in the api key that the developer provides in their config.development.js file.

I have read other stack questions where you can over ride the sync method because its just an ajax request but I haven't seen any that give examples where you can do this or even where you would place this "over write"

What I am looking for is example, documentation anything that lets me pass in extra headers for things like delete, post, put, update and patch and still keep things restful.

The only request that doesn't need the api key is GET requests.

user3379926
  • 3,855
  • 6
  • 24
  • 42
  • You probably want to use [`$.ajaxSetup`](http://api.jquery.com/jquery.ajaxsetup/) instead, and add the appropriate `{headers: {...}}` option – user229044 Aug 27 '14 at 21:10
  • @meagar That would be a nice solution if the OP is using `jQuery` – Kyle Needham Aug 27 '14 at 21:27
  • @KyleNeedham I believe that is (at least) implied by Backbone. Backbone uses `$.ajax` to issue it's AJAX requests; if `$` happens to be, say Zepto instead of jQuery, then `$.ajaxSettings` would be appropriate, but my point stands. – user229044 Aug 27 '14 at 21:51
  • Use this http://stackoverflow.com/questions/11474139/how-to-specify-url-and-header-in-backbone-to-use-crud-method-on-my-model – Eugene Glova Aug 28 '14 at 21:54

1 Answers1

1

You can override Backbone.sync and pass in your options. Make sure to include your override after you include Backbone but before any calls that trigger Backbone.sync.

oldSync = Backbone.sync;

Backbone.sync = function(method, model, options)
{
    // Do some modification to options here and then call the old sync.
    return oldSync(method, model, options);
}

Here is some useful documentation links for you to check out:

Kyle Needham
  • 3,379
  • 2
  • 24
  • 38