我正在尝试在Ember中迭代数组并使用removeObject()
从数组中删除对象。下面的示例只会删除一些对象。我期望它能够迭代所有对象并将它们全部删除:
App = Ember.Application.create();
App.ITEM_FIXUTRES = [
'Item 1',
'Item 2'
];
App.ITEM_FIXTURES = App.ITEM_FIXUTRES.map(function (item) {
return Ember.Object.create({title: item});
});
App.IndexRoute = Ember.Route.extend({
model: function() {
return App.ITEM_FIXTURES;
},
actions: {
add: function(title) {
var items = this.modelFor('index');
items.addObject(Ember.Object.create({title: title}));
this.controller.set('title', '');
},
removeAll: function() {
var items = this.modelFor('index');
items.forEach(function (item) {
// I actually only want to remove certain objects with specific
// properties but this illustrates the issue.
items.removeObject(item);
});
}
}
});
该模板非常简单:
<script type="text/x-handlebars" id="index"></script>
Here is a JSBin: http://jsbin.com/kelinime/4/edit