オブジェクトのコレクションがあるが、それらのオブジェクトに関するより高度な情報も保存したい場合、コレクションにモデルの動作を追加することは適切ですか?
私の状況では、「curPath」というブール フィールドを持つアプリケーション パスのコレクションを探しています。変更された場合、コレクションは現在のページを示すフラグを設定する必要があります。このように、外部のオブザーバーは、パス コレクション内のすべてのモデルではなく、1 つのフィールドのみを観察する必要があります。
これは次のようになります。
var PathModel = Backbone.Model.extend({})
var PathCollection = Backbone.Collection.extend({
initialize: function(){ this.model = PathModel }
})
// I want to be able to set observable properties on the collection, so...
var PathManager = _.extend(Backbone.Model, PathCollection)
// Then maybe I can do something like this?
PathManager.each(function(pathModel){
pathModel.on('change:curPath', function(m, value, o){
// I mean for 'this'.set to point to the instance of the PathManager
if (value === true){ this.set('curPath', pathModel.get('id')) }
}, this)
}, this)
観察可能な動作をコレクション (コレクション + モデル > モデル) に追加するのは適切ですか、それとも全体にラッピング モデルを追加する必要がありますか (モデル > コレクション > モデル)、または他の解決策はありますか?