Marcin Pruciak
Marcin Pruciak

Reputation: 366

How to observe store collection

I have code for generate checkboxes list:

accountsCheckboxes: Ember.computed('accountsCheckboxes.@each', function(){
  return this.model.accounts.map(row => {
    return {
      label: row.get('name'),
      value: row.get('id')
    };
  })
}),

but after modify accounts collection, add or remove, this computed property doesnt refresh. I tried find how to do it with events, or how to observe store collection, but without success. I modyfy this model collection in others controllers.

Upvotes: 1

Views: 369

Answers (1)

nem035
nem035

Reputation: 35491

Its a little confusing what you're trying to do by observing the same property you're defining:

// accountsCheckboxes observes accountsCheckboxes?
accountsCheckboxes: Ember.computed('accountsCheckboxes.@each', ...)

This won't work and will probably result in an infinite chain of lookups.

Did you mean to observe model.accounts instead? If so, this is what you could've done:

accountsCheckboxes: Ember.computed('[email protected]', function() {
  return this.get('model.accounts').map(row => {
    return {
      label: row.get('name'),
      value: row.get('id')
    };
  })
});

Note that you must call this.get('model'), not this.model to make sure you always get the proper data.

Alternatively, you might use Ember.computed.map:

accountsCheckboxes: Ember.computed.map('[email protected]', function(row) {
  return {
    label: row.get('name'),
    value: row.get('id')
  };
});

Upvotes: 1

Related Questions