Backbone models retrieve, update, and destroy data using the fetch , save and destroy methods. These methods delegate the actual part of the request to Backbone.sync. Under the hood, all Backbone.sync does this by creating an ajax request using jQuery. To enable basic HTTP authentication, you have several options.
fetch , save and destroy all accept the optional [options] parameter. These [options] are just a dictionary of jQuery query parameters that are included in the jQuery ajax call that is being executed. This means that you can easily define a simple method that adds authentication:
sendAuthentication = function (xhr) { var user = "myusername";
And include it in every call to fetch , save and destroy . For example:
fetch({ beforeSend: sendAuthentication });
This can create quite a few repetitions. Another option would be to override the Backbone.sync method, copy the source code, and simply include the beforeSend parameter in every jQuery ajax request made.
Hope this helps!
shanewwarren Apr 10 '12 at 3:17 2012-04-10 03:17
source share