我正在使用backbone-tastypie,但我有最艱難的時間才能正常工作。在Tastypie中,我爲我的資源使用了ApiKeyAuthentication,因此每個ajax請求都需要將apikey和用戶名附加到請求的末尾,或發送添加用戶名和api密鑰的其他標頭。如何覆蓋Backbone.sync,以便在最後添加apikey和用戶名?
我想刪除一個視圖,並使用骨幹用下面的代碼它的型號:
// Remove the goal update view from the DOM
removeItem: function() {
this.model.destroy({wait: true, success: function() {
console.log("success");
}, error: function() {
console.log("error");
}});
},
功能執行後,瀏覽器嘗試做以下網址的GET請求:
:8000/api/v1/update/2/
它最後沒有包含api_key或用戶名,並且在url的末尾有一個尾部的斜線。我認爲它試圖使用Backbone.oldSync來執行GET請求。我如何做到這一點,所以同步確實在最後包含了用戶名/ api鍵並刪除了最後一個斜槓?
在所有其他的請求,我做了這麼API密鑰和用戶名被追加到HTTP請求的末尾加入以下代碼骨幹-tastypie:
if (!resp && (xhr.status === 201 || xhr.status === 202 || xhr.status === 204)) { // 201 CREATED, 202 ACCEPTED or 204 NO CONTENT; response null or empty.
var location = xhr.getResponseHeader('Location') || model.id;
return $.ajax({
url: location + "?" + "username=" + window.app.settings.credentials.username + "&api_key=" + window.app.settings.credentials.api_key,
success: dfd.resolve,
error: dfd.reject,
});
}
可能重複的[如何重寫Backbone.sync?](http://stackoverflow.com/questions/5096549/how-to-override -backbone-sync) – Ben