Quantcast
Viewing all articles
Browse latest Browse all 4828

Ember.computed -- declaring additional dependencies (for example, Ember.computed.filter)

@dlindquist-egistix wrote:

When using the Ember.computed helpers, there are case in which it becomes necessary to declare additional dependencies. For example, when using Ember.computed.filter:

dogs: ...an array of dogs we load from somewhere...,
desiredColor: 'brown',
filteredDogs: Ember.computed.filter('dogs',function(item) {
    const wantedColor = this.get('desiredColor');
    return item.get('coat') === wantedColor;
})

This example works perfectly, including when I change the value of desiredColor… except that desiredColor cannot be declared as a dependency, so the filteredDogs property doesn’t recompute.

It is possible to make use of the old Ember 1.12 syntax to ‘fix’ this – for example:

dogs: ...an array of dogs we load from somewhere...,
desiredColor: 'brown',
filteredDogs: Ember.computed.filter('dogs',function(item) {
    const wantedColor = this.get('desiredColor');
    return item.get('coat') === wantedColor;
}).property('dogs.@each.coat','desiredColor')

but this seems like a bad idea, since I’m guessing the .property method will be removed at some point.

Is there any “proper” way to do this? Does the Ember team have any opinion on how this should be accomplished? Or is it necessary to write your own computed property for each such case?

Thanks in advance!

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4828

Trending Articles