Ember Computed properties and Handlebars templates


#1

In my Ember Controller, I have 2 computed properties (CP) as below;

itemsWithData: function() {
    var dataItems = [];

    return dataItems;
}.property('containsFailure'),
someArray: function() {
    var items = this.get('itemsWithData');
    var someArray = [];

    return someArray;
}.property('itemsWithData')

Now in my Ember Handlerbars template, while I only use someArray CP to iterate on and display values, I am not using the other CP (i.e. itemsWithData)

But if I do not refer itemsWithData in my template, the same is not getting executed in my controller (so even someArray does not execute since it depends on itemsWithData)

It only executes if I explictly add a dummy reference as below;

{{#each itemsWithData as |data|}}
{{/each}}

Is that how CP works in controllers/templates ? I need a way such that I do not have to add this dummy code in my template.


#2

I am a bit confused:

  1. What do you mean by “executed”. Do you mean the value is wrong, or you expected some side effect that did not happen?
  2. The specifics are likely to depend on the implementation of your properties, so it might actually be helpful to post them. Maybe on a gist if they’re long.
  3. I think it is very likely that your computed properties does not list its dependencies correctly. For instance, if you want to recompute it when items are added/removed to a dependent key, you should say so explicitly (for instance, .property('someArray.[]'))

#4

Thats not correct!

If you loop over someArray in your template, itemsWithData will be executed/calculated before someArray, and then someArray will be executed/calculated and later displayed.

Can you provide a full not working example, because I think your demo code would work as expected.