RLW
RLW

Reputation: 132

Controller computed property based on model only valid in loop

I have a the following in Ember

Route

model: function() {
  return this.store.findAll('competency');
}

Controller

calendarItems: Ember.computed('model', function() {
  return this.get('model').map(function(competency) {
    return {
      'name': competency.get('title'),
      'start': competency.get('endDate')
    };
  });
})

Template

{{#each model as |item|}}
  {{log calendarItems}}
{{/each}}

{{log calendarItems}}

For some reason unknown to me the {{log calendarItems}} inside the loop displays correctly with all of the store items in the models mapped correctly. But only when the {{log calendarItems}} is not present outside the loop.

When the {{log calendarItems}} is also present outside the loop it causes all 4 log statements to return [] as though the model had nothing to map.

If {{log calendarItems}} is on its own it also returns [].

Am I missing something fundamental about Ember here?

Thanks in advance, Ryan

Upvotes: 1

Views: 275

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

This won't necessarily fix the logging, but it should fix the computed property in that it should update as records become available (if the real problem is that the objects are loading asynchronously, which is kind of my suspicion)

calendarItems: Ember.computed('model.@each.{title,endDate}', function() {
  return this.get('model').map(function(competency) {
    return {
      'name': competency.get('title'),
      'start': competency.get('endDate')
    };
  });
})

Upvotes: 1

Related Questions