How to run action in Ember Controller afterRender

Mohan Kumar picture Mohan Kumar · Jun 17, 2015 · Viewed 14k times · Source

I am new to ember framework. I just want to execute a function that is defined inside the actions hook after the rendering completes.

var Controller = Ember.Controller.extend({
  actions: {
    foo: function() {
        console.log("foo");
    }
  }
});
Ember.run.schedule("afterRender",this,function() {
  this.send("foo");
}

But the above code is not working. I just want to know, is it possible to run foo() afterRender?

Answer

artych picture artych · Jun 17, 2015

You could use init:

App.Controller = Ember.Controller.extend({
  init: function () {
    this._super();
    Ember.run.schedule("afterRender",this,function() {
      this.send("foo");
    });
  },

  actions: {
    foo: function() {
      console.log("foo");
    }
  }
});