Slevin
Slevin

Reputation: 4222

Observe property and run code after View is inDOM

I'm observing a property on my view to draw charts. The problem: the observer triggers before the view is inDOM.

Is there a way to wait until the state changes?

App.ReportsView = Ember.View.extend({

  drawChart: function() {

    console.log(this.get('state'));
    // draw some charts

  }.observes('controller.chartData.projects')

});

Upvotes: 0

Views: 165

Answers (1)

ppcano
ppcano

Reputation: 2861

View has a state property where you can check if it is in DOM, but it is not a public API.

You could set a property in your view when it has been inserted in DOM by listening the didInsertElement event.

This could be a possible approach (not tested):

App.ReportsView = Ember.View.extend({

  setInDOM: function(){

    this.set('isInDom', true);
    this.drawChart();

  }.on('didInsertElement'),

  tryDrawChart: function() {

    if ( this.get('isInDom') ) {
      this.drawChart();
    } 

  }.observes('controller.chartData.projects'),

  drawChart: function() {

  }

});

Upvotes: 1

Related Questions