バックボーン1.1.0を使用しているIm。私はBackboneを使用して以来、しばらくしていましたが、私は以前はsaveメソッドの成功ハンドラを簡単に上書きできることは確かです。しかし、今私はそれを行うように見えることはできません!私のコードは次のとおりです。Cannnotはバックボーン保存成功ハンドラを上書きします
model.save({}, {
successs: function() {
console.log('in my custom success handler');
}
});
マイカスタムハンドラは実行されないdoesntの、デフォルトの成功ハンドラはsync
イベントをトリガーする、しません。
私は質問hereを見て、いずれもうまくいきませんでした。方法保存モデルのバックボーンライブラリコード(V1.1.0)
等などなど、最初のパラメータとしてnullを渡し、三番目のパラメータ、第二パラメータに成功ハンドラオブジェクトに渡す含まこれらは、次のとおり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;
},
2の事は私をパズル:
あなたが成功ハンドラに渡すときので、今までに、(私はそれを行うことができるように使用されると確信している)成功ハンドラを上書きすることを可能にされている可能性がどのように1/aはローカル変数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
は私の顧客のハンドラメソッドが含まれていること。しかし、地元のVAR success
は、どういうわけか、undefined
です....
この質問を作成している間、それはタイプミスだ場合、私は知りませんが、あなたは '指定しました成功 - その物件に3文字の「s」があります。 – Mjh
* facepalm *これは問題だった – Mark
まあ、少なくともそれはかなりシンプルでした(しかし、まだあなたのプロジェクトに幸運を祈る!) – Mjh