Marionette.js を使用するように Backbone.js アプリケーションをリファクタリングしていますCollectionView
。
ItemView
モデルにいくつかの があるとしますCow
:
// Declare my models.
var Cow = Backbone.Model.extend({});
var Cows = Backbone.Collection.extend({
model: Cow
});
// Make my views
var GrassPatch = Marionette.ItemView.extend({
tagName: 'div',
template: "<section class='grass'>{{name}}</section>",
})
var Pasture = Marionette.CollectionView.extend({});
// Instantiate the CollectionView,
var blissLand = new Pasture({
itemView: GrassPatch;
});
// Now, add models to the collection.
Cows.add({
name: 'Bessie',
hasSpots: true
});
Cows.add({
name: 'Frank',
hasSpots: false
});
ここにトリックがあります。牧草地に斑点のある牛だけが欲しい. hasSpots
CollectionView (Pasture) を定義する際に、 ===のモデルにのみ注意を払うように指示するにはどうすればよいtrue
ですか?
理想的には、すべてのイベントで CollectionView フィルターを使用したいと考えていますが、最小限、モデル プロパティに基づいて一部の ItemView のみをレンダリングするにはどうすればよいですか?
アップデート
私は David Sulc の例を使用しましたが、これは簡単な解決策になりました。実装例を次に示します。
this.collection = Backbone.filterCollection(this.collection, function(criterion){
var len = String(criterion).length;
var a = criterion.toLowerCase();
return function(model){
var b = String(model.get('name')).substr(0, len).toLowerCase();
if (a === b) {
return model;
}
};
});
this.collection.add({ name: 'foo' });
this.collection.add({ name: 'foosball' });
this.collection.add({ name: 'foo bar' });
this.collection.add({ name: 'goats' });
this.collection.add({ name: 'cows' });
this.collection.filter('foo');
// -> returns the first three models