これは、1つは試してみて、説明するのも楽しいです:)
あなたはこのようon
を呼び出すときに:
this.fModel.on('change', this.bCollection.fetch, this);
あなたはfetch
がどんなthis
であるにして実行されていることをコンテキストを設定しています。このコードでは、this
は単純にトップレベルのアプリケーションまたは類似のものです。 fetch
はそれほどできません!だから私たちは基本的にvar collection = this;
にそれを作る
// Fetch the default set of models for this collection, resetting the
// collection when they arrive. If `add: true` is passed, appends the
// models to the collection instead of resetting.
fetch: function(options) {
options = options ? _.clone(options) : {};
if (options.parse === undefined) options.parse = true;
var collection = this;
var success = options.success;
options.success = function(resp, status, xhr) {
collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
if (success) success(collection, resp);
};
options.error = Backbone.wrapError(options.error, collection, options);
return (this.sync || Backbone.sync).call(this, 'read', this, options);
},
... おっと:さんはfetch
の実装を見てみましょう!
をfetch
に設定しました。トップレベルのアプリケーションに設定しました。
だから、あなたはそれをラップするとき、それは働く理由はもっと楽しいです:
var wrapped = function() { this.bCollection.fetch(); };
this.fModel.on('change', wrapped, this);
我々はthis
するwrapped
のコンテキストを設定しました。 this.bCollection
はまさに私たちが望むものなので、それは問題ありません。しかし、ここでをbCollection
と呼ぶと、普通のやり方で、その中にthis
というオブジェクトがバインドされています。これは通常のjavascriptのものです。
だから、ここTLです; DR:
あなたが実際にしたい:
this.fModel.on('change', this.bCollection.fetch, this.bCollection);
fetch
関数呼び出しのコンテキストはコレクション自体、そして他には何もする必要がありますので。
意味がありますか?
乾杯:
優秀な説明。本当にありがとう! – MrJ
うれしかった! – rfunduk