2016-11-07 36 views
-1

使用骨幹網1.1.0。自從我使用Backbone以來,它已經有一段時間了,但我確定我曾經能夠輕鬆地覆蓋保存方法的成功處理程序。但是,現在我似乎無法做到!我的代碼是:無法覆蓋Backbone保存成功處理程序

model.save({}, { 
    successs: function() { 
     console.log('in my custom success handler'); 
    }   
}); 

我的自定義處理犯規不執行,默認的成功處理程序,觸發sync事件。

我看了一下here這個問題,並嘗試了各種解決方案,但都沒有成功。這些包括傳遞成功處理程序對象在第三參數,第二參數,並傳遞零作爲第一個參數等等等等等等

的骨幹庫代碼(V1.1.0),用於模型保存方法是:

save: function(key, val, options) { 
    var attrs, method, xhr, attributes = this.attributes; 

    // Handle both `"key", value` and `{key: value}` -style arguments. 
    if (key == null || typeof key === 'object') { 
    attrs = key; 
    options = val; 
    } else { 
    (attrs = {})[key] = val; 
    } 

    options = _.extend({validate: true}, options); 

    // If we're not waiting and attributes exist, save acts as 
    // `set(attr).save(null, opts)` with validation. Otherwise, check if 
    // the model will be valid when the attributes, if any, are set. 
    if (attrs && !options.wait) { 
    if (!this.set(attrs, options)) return false; 
    } else { 
    if (!this._validate(attrs, options)) return false; 
    } 

    // Set temporary attributes if `{wait: true}`. 
    if (attrs && options.wait) { 
    this.attributes = _.extend({}, attributes, attrs); 
    } 

    // After a successful server-side save, the client is (optionally) 
    // updated with the server-side state. 
    if (options.parse === void 0) options.parse = true; 
    var model = this; 
    var success = options.success; 
    options.success = function(resp) { 
    // Ensure attributes are restored during synchronous saves. 
    model.attributes = attributes; 
    var serverAttrs = model.parse(resp, options); 
    if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs); 
    if (_.isObject(serverAttrs) && !model.set(serverAttrs, options)) { 
     return false; 
    } 
    if (success) success(model, resp, options); 
    model.trigger('sync', model, resp, options); 
    }; 
    wrapError(this, options); 

    method = this.isNew() ? 'create' : (options.patch ? 'patch' : 'update'); 
    if (method === 'patch') options.attrs = attrs; 
    xhr = this.sync(method, this, options); 

    // Restore attributes. 
    if (attrs && options.wait) this.attributes = attributes; 

    return xhr; 
}, 

兩件事困擾我:

1 /怎麼可能永遠都沒有可能改寫成功處理程序(我敢肯定,我曾經是能夠做到這一點),因爲當你在成功處理程序傳遞,它得到一個分配給本地變種success,然後覆蓋:

var success = options.success; 
    options.success = function(resp) { 
    .... 

2 /爲什麼我的處理程序也不能執行?它應該得到分配給本地succss VAR:

var success = options.success; 

,然後在options.success執行:

if (success) success(model, resp, options); 

當我通過Chrome開發者工具調試,成功是不確定的。但我可以看到:

var success = options.success; 

options.success包含我的客戶處理程序方法。然而,局部變量success是,不知何故,undefined ....

+2

我不知道這是否是一個錯字,同時創造了這個問題,但您指定'成功「 - 在該房產中有3個字母's'。你的回調定義也是錯誤的,它是'成功:函數(){}',而不是'成功:功能({});' – Mjh

+0

* facepalm *這是問題 – Mark

+1

嗯,至少它是一些相當簡單棘手的現貨),祝你的項目好運! – Mjh

回答

1

我覺得你的代碼應該是:

model.save({}, { 
    success: function(){ 
    //^-----this-----^ 
    console.log('in my custom success handler'); 
    }   
});