I've been reading through the docs and API and I'm having a hard time finding an explanation for the following:
export default Ember.Controller.extend({
collectionTotal: function() {
var games = this.get('model');
return games.length
}.property('@each')
});
What is actually happening with the .property('@each')? I know I am getting the computed property back, I don't understand what @each is.
What is @each?
@each observes individual properties on each item in an array.
For example, if I observe [email protected], I will receive an event if:
users property is replaced e.g. this.set('users', ...)usersname property changes on any of the itemsYou can observe multiple properties using this syntax: users.@each.{name,email}
You cannot nest them. This will not work: [email protected][email protected]
Read more in the official documentation:
To answer your question
@each by itself doesn't make sense. You can observe the [] property if you only need to watch for items being added or removed.
Typically you should observe the same properties that are used in the body of the function. In your example that would be model and length:
collectionTotal: function() {
var games = this.get('model');
return games.get('length');
}.property('model.length')
Or equivalently:
collectionTotal: function() {
return this.get('model.length');
}.property('model.length')
Or equivalently:
collectionTotal: Ember.computed.reads('model.length')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With