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