我正在尝试允许用户搜索集合中的displayNames和电子邮件。
到目前为止,我的整个复合视图看起来像下面的代码。这会呈现并记录我的var搜索,但我不确定如何使用collection.where和新的Backbone.Collection后,我是否调用render?
define(["marionette", "text!app/templates/bamboo/employees/collection.html", "app/collections/bamboo/employees",
"app/views/bamboo/employees/item", "jquery"],
function(Marionette, Template, Collection, Row, $) {
"use strict"
return Backbone.Marionette.CompositeView.extend({
template: Template,
itemView: Row,
itemViewContainer: "ul",
collectionEvents: {
'sync': 'hideLoading'
},
events: {
'keyup #filter-input': 'initialize'
},
initialize: function () {
var search = $('#filter-input').val()
if (typeof(search) != "undefined") {
console.log(search)
var filtered = //somehow search collection displayName and email by value search
this.collection = new Backbone.Collection(filtered);
} else {
this.showLoading()
this.collection = new Collection()
return this.collection.fetch()
}
},
showLoading: function() {
this.$el.addClass('loading')
},
hideLoading: function() {
this.$el.removeClass('loading')
}
})
})
答案 0 :(得分:2)
您可以使用Marionette
从CollectionView
CompositeView
和view.children._views
获取观看次数。
使用这样的代码:
_.each(colView.children._views,function(v){
if(!condition){
v.$el.hide();
}
});
您可以隐藏colView
中没有条件的视图(在您的情况下条件可能为v.model.get('type') == 'todo'
)。
答案 1 :(得分:1)
我认为如果您只实例化集合的单个实例,然后在模型上调用函数,这可能会更容易。
return Backbone.Marionette.CompositeView.extend({
template: Template,
itemView: Row,
itemViewContainer: "ul",
events: {
'keyup #filter-input': 'filter'
},
initialize: function() {
this.filter();
},
filter: function () {
var search = $('#filter-input').val() || '';
this.collection.invoke('set', 'filter', search);
},
// ...
});
然后,对于你的itemViews
Row = Backbone.Marionette.ItemView.extend({
modelEvents: {
"change filter": "filter"
},
filter: function(model, search) {
if (model.shouldBeShown(search)) {
this.$el.show();
} else {
this.$el.hide();
}
}
});