Backbone Todo MVC sourceでは、関数のネイティブのapplyメソッドを使用して を使用せずに Underscore メソッドを呼び出していますが、なぜそれが必要なのかわかりません。
// Filter down the list of all todo items that are finished.
completed: function() {
return this.filter(function( todo ) {
return todo.get('completed');
});
},
// Filter down the list to only todo items that are still not finished.
remaining: function() {
return this.without.apply( this, this.completed() );
},
withoutの呼び出しは、 filterなどの他の Underscore メソッドと比較して場違いに見えます。バックボーン ソースを再確認して、 Collection オブジェクトに別の方法で混在していないことを確認しました。案の定、そうではありません。
これは、下線メソッドが Collection にアタッチされる方法です。
_.each(methods, function(method) {
Collection.prototype[method] = function() {
var args = slice.call(arguments);
args.unshift(this.models);
return _[method].apply(_, args);
};
});
予想どおり - Collection のモデルはすでに最初の引数として渡されています。さらに、メソッドは Collection オブジェクトで呼び出されるため、これは正しくバインドされます。
メソッドを次のように変更してこれを確認しました
this.without(this.completed());
そして、それはうまくいきます。
私はここで何を見落としていますか?