Prajwal Boloor
Prajwal Boloor

Reputation: 23

Ember computed property from array

I am trying to include a computed flag in my model when a particular string exists in an included array.

userType: DS.attr(),   // ['Don','Ben','Dec', 'Con']

isDon: Ember.computed('userType', function() {
return (!((this.get('userType')).includes('Don'));
 }).property('userType'),

any help ?

Upvotes: 2

Views: 956

Answers (2)

Prajwal Boloor
Prajwal Boloor

Reputation: 23

for some strange reason, userType was not treated as an array so I had to create a computed property like

userRoles: Ember.computed('userType.[]', function () {
return Ember.A(this.get('userType'));
}),
isDon: Ember.computed('userRoles', function () {
return this.get('userRoles').filter((item) => item == 'Don').length > 0;
}),

Upvotes: 0

Ember Freak
Ember Freak

Reputation: 12872

Your syntax is wrong. It looks you are mixing the two different way of defining computed properties,

isDon: Ember.computed('userType', function() {
  return (!((this.get('userType')).includes('Don'));
 }),

If you want to recalculate isDon every when you add/remove elements from userType then consider the dependant key as userType.[]

Refer
https://guides.emberjs.com/v2.13.0/object-model/computed-properties/ https://guides.emberjs.com/v2.13.0/object-model/computed-properties-and-aggregate-data/#toc_code-code-vs-code-each-code

Upvotes: 1

Related Questions