Search code examples
javascriptember.jsobservers

ember js component observer does not work


I have a scenario in emberjs component where the observe does not get hit. I figured out the reason as "the component is not yet inserted when the component property being observed is set."

My questions is, couldn this be handled in a better way in ember js?

Better explanation can be found in the below jsbin`s.

Not working Scenario

Working scenario


Solution

  • You can specify .on('init') to force the observers to run right after initialization; otherwise like @Kingpin2k mentioned - they don't run

    App.TextboxDisplayComponent = Ember.Component.extend({
      displayText: '',        
    
      boundProperty: '',
    
      observeBoundProperty: function () {
        this.set('displayText', this.get('boundProperty'));        
      }.observes('boundProperty').on('init')
    });
    

    Your (non-)working example here