Backbone将其XHR请求委托给jQuery / Zepto,这是您必须修改的内容.
最简单的解决方案可能是通过$.ajaxSetup和headers options提供默认选项
headers(added 1.5)
Default: {}
A map of additional header key/value pairs to send along with the
request. This setting is set before the beforeSend function is called;
therefore, any values in the headers setting can be overwritten from
within the beforeSend function.
你会这样设置:
$.ajaxSetup({
headers:{
X_REST_USERNAME: "admin@restuser",
X_REST_PASSWORD: "admin@Access"
}
});
或者您可以为每个请求传递其他选项,Backbone会将它们转发给jQuery:
var m = new Backbone.Model();
m.fetch({
headers:{
X_REST_USERNAME: "admin@restuser",
X_REST_PASSWORD: "admin@Access"
}
});
最后,您可以覆盖Backbone.sync以为每个请求添加标头:
Backbone.realsync = Backbone.sync;
Backbone.sync = function(method, model, options) {
options || (options = {});
options.headers = {
X_REST_USERNAME: "admin@restuser",
X_REST_PASSWORD: "admin@Access"
};
return Backbone.realsync.call(model, method, model, options);
};
var m = new Backbone.Model();
m.fetch();