Marionette.js CollectionView,仅渲染特定的模型
Marionette.js CollectionView, only render specific models
我正在重构我的Backbone.js应用程序以使用Marionette.js,并且我正试图用CollectionView
来解决问题。
假设我有几个型号为Cow
:的ItemView
// 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
});
下面是诀窍。我只想要牧场上有斑点的奶牛。在定义我的CollectionView(牧场)时,我如何告诉它只关注那些hasSpots
===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
Marionette在v2.4.1中为您处理此问题。
请参阅CollectionView.filter方法。
以下来自文档:
var cv = new Marionette.CollectionView({
childView: SomeChildView,
emptyView: SomeEmptyView,
collection: new Backbone.Collection([
{ value: 1 },
{ value: 2 },
{ value: 3 },
{ value: 4 }
]),
// Only show views with even values
filter: function (child, index, collection) {
return child.get('value') % 2 === 0;
}
});
// renders the views with values '2' and '4'
cv.render();
// change the filter
cv.filter = function (child, index, collection) {
return child.get('value') % 2 !== 0;
};
// renders the views with values '1' and '3'
cv.render();
// remove the filter
// note that using `delete cv.filter` will cause the prototype's filter to be used
// which may be undesirable
cv.filter = null;
// renders all views
cv.render();
正如其他人所建议的,实现这一点的最佳方法是过滤集合,使其仅包含要显示的模型,并将fitlered集合传递给CollectionView进行渲染。
您可以在此处看到一个工作示例:http://davidsulc.github.io/marionette-gentle-introduction/#contacts使用右上角的输入字段筛选联系人,以仅显示包含该文本的模型(例如"li")。
这是通过使用一种处理过滤的特殊集合类型来实现的:https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/entities/common.js
它在这里被实例化:https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js#L13
这个代码来自我关于木偶的书。
@Will M过滤集合的建议是实现这一点的合适方法。
有时由于某些自定义逻辑,您无法筛选集合,并且您希望这些模型在集合中,但不希望它们被渲染。要实现它,您可以:
var Pasture = Marionette.CollectionView.extend({
addChild: function(child, ChildView, index) {
if(child.get('hasSpots')) {
return Marionette.CollectionView.prototype.addChild.call(this, child, ChildView, index);
}
}});
尽管我同意过滤集合是一种更好的方法。
- 如何使用backbone.js从集合中获取模型名称
- 骨干模型默认值-todos.js示例中不必要的代码
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- Ext.js从json构建模型关系的问题
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- spine.js在启动时填充模型实例
- Three.JS OBJ模型未显示
- Backbone.js将模型绑定到视图时出错
- 使用树模型 js 异步树遍历
- 在 ember 上复制嵌套模型.js(成功保存后)
- 如何获取语言模型JS文件
- 在鼠标点击位置添加3D模型.js
- 如何将实例方法添加到帆中的所有模型.js
- 导出 MONGO 数据库连接到模型.js文件
- 在骨干网中更改视图的模型.js
- 使Node.js与树模型js协同工作
- 使用没有视图模型. js文件的knockout组件
- 骨干中的嵌套模型.js,如何接近
- 如何将值从集合传递到主干中的模型.js
- 在 Backbone 中查找模型.js按 cid 而不是 id 收集