1. 一般,你可能有一个计算的属性依赖于数组中的全部元素来肯定它的值。例如,你可能想要计算controller中全部todo items的数量,以此来肯定完成了多少任务。数组
export default Ember.Controller.extend({ todos: [ Ember.Object.create({ isDone: true }), Ember.Object.create({ idDone: false }), Ember.Object.create({ isDone: true }) ], remaining: Ember.computed('todos.@each.isDone', function () { var todos = this.get('todos'); return todos.filterBy('isDone', false).get('length');//1 }); });
import TodosController from 'app/controllers/todos'; todosController = TodosController.create(); todosController.get('remainging');
2. 若是我改变todo's isDone属性, remaining属性将会被自动更新:app
var todos = todosController.get('todos'); var todo = todos.objectAt(1); todo.set('isDone', true); todosController.get('remaining'); //0 todo = Ember.Object.Create({ isDone: false }); todos.pushObject(todo); todosController.get('remaining');//1
3. 请注意@each不能嵌套。this
正确:todos@each.owner.namespa
错误:todos@each.owner.@each.namecode