smoyth
smoyth

Reputation: 709

Observing controller property from view only works if get('controller') called from didInsertElement

Note the below Ember view definition. If I remove the didInsertElement call or comment out the get('controller') call, the setupMultiselect observer never gets called. Is this a feature or a bug? Confused...

Discourse.KbRelatedObjView = Discourse.View.extend({

  ...

  didInsertElement: function() { var self = this;
    // for some reason this needs to be here else the observer below never fires
    self.get('controller');
  },

  setupMultiselect: function() { var self = this;

    ...

  }.observes('controller.objPage')

});

Upvotes: 2

Views: 805

Answers (1)

GJK
GJK

Reputation: 37369

I wouldn't say it's a feature or a bug, more like a quirk. It is the expected behavior though. It's noted here.

UNCONSUMED COMPUTED PROPERTIES DO NOT TRIGGER OBSERVERS

If you never get a computed property, its observers will not fire even if its dependent keys change. You can think of the value changing from one unknown value to another.

This doesn't usually affect application code because computed properties are almost always observed at the same time as they are fetched. For example, you get the value of a computed property, put it in DOM (or draw it with D3), and then observe it so you can update the DOM once the property changes.

If you need to observe a computed property but aren't currently retrieving it, just get it in your init method.

Upvotes: 2

Related Questions