[英]Backbone: calling a success with a custom sync method
我的backbone.js
應用程序中有自定義sync
方法。 我的所有模型都調用此方法,但由於我在此方法中取代了success
,因此不再調用單個模型中的成功方法。 這就是我的意思 - 以下是我的自定義同步方法:
app.customSync = function(method, model, options) {
var success = options.success,
error = options.error,
customSuccess = function(resp, status, xhr) {
//call original, trigger custom event
if(con)console.log('in custom success');
success(resp, status, xhr);
},
customError = function(resp, status, xhr) {
if(con)console.log('in custom error');
error(resp, status, xhr);
};
options.success = customSuccess;
options.error = customError;
Backbone.sync(method, model, options);
};
Backbone.Model.prototype.sync = app.customSync;
以下是我嘗試從模型中調用成功的示例:
this.model.save({
success:function(model, response){
if(con)console.log('this is never called');
}
});
有誰知道我仍然可以使用自定義成功方法自定義同步,並從我的個人保存中調用成功?
作為旁注,我嘗試在msuccess
中調用success
model.save
,但msuccess
在自定義同步中未定義。
Model.save的第一個參數是您希望修改的屬性的哈希,選項是第二個並保存成功/錯誤回調。
嘗試
this.model.save({}, {
success: function() {
console.log('save success');
}
});
在工作中看到這個小提琴http://jsfiddle.net/nikoshr/XwfTB/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.