Search code examples
ember.jsember-dataember-cli

How to run action in Ember Controller afterRender


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?


Solution

  • 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");
        }
      }
    });