我有一个CompositeView,它是一个选项框,可以输出选项作为childViews,我希望只能输出模型属性viewable
为true
的选项,但我不知道如何实现这在每个生成的元素上。我可以移动模板内部的option
然后将其包装在条件中,但是会有一个生成的div
会破坏select的语义。任何人都可以建议我应该如何处理这个问题?
JS
var people = [{
id: 1,
name: 'Kyle',
viewable: true
}, {
id: 2,
name: 'Peter',
viewable: false
}, {
id: 3,
name: 'Simon',
viewable: true
}];
// Create a Person Model
var Person = Backbone.Model.extend({});
// Create a People Collection
var People = Backbone.Collection.extend({
model: Person
});
// Create a TableRowView -> ItemView
var Row = Marionette.ItemView.extend({
template: '#row',
tagName: 'option'
});
// Create a TableView -> CompositeView
var Overall = Marionette.CompositeView.extend({
template: '#overall',
childView: Row,
childViewContainer: '.js-list',
events: {
'click .js-switch': 'onSwitchClick'
},
templateHelpers: function() {
return {
viewable: this.isViewable
}
},
initialize: function() {
console.log(this.collection.toJSON());
this.isViewable = this.collection.filter(function(model){
return model.attributes.viewable === true;
});
console.log(this.isViewable);
},
onSwitchClick: function(event) {
event.preventDefault();
// Show all Only tem
this.$('.js-list').select2();
}
});
var region = new Backbone.Marionette.Region({
el: '#region'
});
// Setup
var newPeople = new People(people);
var overall = new Overall({
collection: newPeople
});
region.show(overall);
模板
<script type="text/html" id="overall">
<h2>Heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.Esse iste recusandae quisquam voluptas dolorum, fugiat quaerat, dicta eum cupiditate rerum quas expedita quasi officiis quia harum quo, laborum vel debitis.. </p>
<select class="js-list"></select>
<button class="js-switch">Switch</button>
</script>
<script type="text/html" id="row">
<%- name %>
</script>
答案 0 :(得分:1)
Collection.filter
到resque。
只需将filter
添加到Overall
CompositeView
:
filter: function (child) {
return child.get('viewable')
}
以下是使用过滤器的更新示例:http://jsfiddle.net/yuraji/hkdmz4oq/
这是一个简化的工作示例:
var people = [{
name: 'Kyle', viewable: true
}, {
name: 'Peter', viewable: false
}, {
name: 'Simon', viewable: true
}];
var Person = Backbone.Model.extend();
var People = Backbone.Collection.extend({
model: Person
});
var PersonView = Mn.ItemView.extend({
render: function(){
this.$el.html( this.model.get('name') );
return this;
}
});
var PeopleView = Mn.CollectionView.extend({
childView: PersonView,
/**
`filter` method is available in a CollectionView,
as well as in a CompositeView, which extends from CollectionView
*/
filter: function(child, index, collection){
return child.get('viewable');
}
});
var peopleView = new PeopleView({
collection: new People(people)
});
peopleView.render().$el.appendTo(document.body);
&#13;
body * {
border: 1px solid gray;
margin: 2px;
}
&#13;
<script src='http://code.jquery.com/jquery.js'></script>
<script src='http://underscorejs.org/underscore.js'></script>
<script src='http://backbonejs.org/backbone.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/backbone.marionette/2.4.3/backbone.marionette.js'></script>
&#13;