Computed Properties and Aggregate Data Edit Page
Sometimes you have a computed property whose value depends on the properties of
items in an array. For example, you may have an array of todo items, and want
to calculate how many remain incomplete based on their isDone
property.
To facilitate this, Ember provides the @each
key illustrated below:
app/components/todos.js | |
1 2 3 4 5 6 7 8 9 10 11 12 |
export default Ember.Component.extend({ todos: [ Ember.Object.create({ isDone: true }), Ember.Object.create({ isDone: 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'); }) }); |
Here, the dependent key todos.@each.isDone
instructs Ember.js to update bindings
and fire observers when any of the following events occurs:
- The
isDone
property of any of the objects in thetodos
array changes. - An item is added to the
todos
array. - An item is removed from the
todos
array. - The
todos
property of the component is changed to a different array.
Ember also provides a computed property macro
computed.filterBy
,
which is a shorter way of expressing the above computed property:
app/components/todos.js | |
1 2 3 4 5 6 7 8 9 |
export default Ember.Component.extend({ todos: [ Ember.Object.create({ isDone: true }), Ember.Object.create({ isDone: false }), Ember.Object.create({ isDone: true }) ], remaining: Ember.computed.filterBy('todos', 'isDone', false) }); |
In both of the examples above, the remaining
count is 1
:
1 2 3 4 5 |
import TodosComponent from 'app/components/todos'; let todosComponent = TodosComponent.create(); todosComponent.get('remaining'); // 1 |
If we change the todo's isDone
property, the remaining
property is updated
automatically:
1 2 3 4 5 6 7 8 9 10 11 12 |
let todos = todosComponent.get('todos'); let todo = todos.objectAt(1); todo.set('isDone', true); todosComponent.get('remaining'); // 0 todo = Ember.Object.create({ isDone: false }); todos.pushObject(todo); todosComponent.get('remaining'); // 1 |
Note that @each
only works one level deep. You cannot use nested forms like
todos.@each.owner.name
or todos.@each.owner.@each.name
.
Sometimes you don't care if properties of individual array items change. In this
case use the []
key instead of @each
. Computed properties dependent on an array
using the []
key will only update if items are added to or removed from the array,
or if the array property is set to a different array. For example:
app/components/todos.js | |
1 2 3 4 5 6 7 8 9 10 11 12 |
export default Ember.Component.extend({ todos: [ Ember.Object.create({ isDone: true }), Ember.Object.create({ isDone: false }), Ember.Object.create({ isDone: true }) ], selectedTodo: null, indexOfSelectedTodo: Ember.computed('selectedTodo', 'todos.[]', function() { return this.get('todos').indexOf(this.get('selectedTodo')); }) }); |
Here, indexOfSelectedTodo
depends on todos.[]
, so it will update if we add an item
to todos
, but won't update if the value of isDone
on a todo
changes.
Several of the Ember.computed macros
utilize the []
key to implement common use-cases. For instance, to
create a computed property that mapped properties from an array, you could use
Ember.computed.map
or build the computed property yourself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const Hamster = Ember.Object.extend({ excitingChores: Ember.computed('chores.[]', function() { return this.get('chores').map(function(chore, index) { return `CHORE ${index}: ${chore.toUpperCase()}!`; }); }) }); const hamster = Hamster.create({ chores: ['clean', 'write more unit tests'] }); hamster.get('excitingChores'); // ['CHORE 1: CLEAN!', 'CHORE 2: WRITE MORE UNIT TESTS!'] hamster.get('chores').pushObject('review code'); hamster.get('excitingChores'); // ['CHORE 1: CLEAN!', 'CHORE 2: WRITE MORE UNIT TESTS!', 'CHORE 3: REVIEW CODE!'] |
By comparison, using the computed macro abstracts some of this away:
1 2 3 4 5 |
const Hamster = Ember.Object.extend({ excitingChores: Ember.computed.map('chores', function(chore, index) { return `CHORE ${index}: ${chore.toUpperCase()}!`; }) }); |
The computed macros expect you to use an array, so there is no need to use the
[]
key in these cases. However, building your own custom computed property
requires you to tell Ember.js that it is watching for array changes, which is
where the []
key comes in handy.