How to run action in Ember Controller afterRender

14,617

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");
    }
  }
});
Share:
14,617
Mohan Kumar
Author by

Mohan Kumar

Updated on June 06, 2022

Comments

  • Mohan Kumar
    Mohan Kumar almost 2 years

    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?

  • Roger
    Roger about 8 years
    the solution may cause test error "Assertion Failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run". How to walk around this?
  • Scott Fanetti
    Scott Fanetti almost 7 years
    When in testing mode Ember does not auto start a run loop. You can create a run look explicitly by wrapping the schedule call in an Ember.run() and it will work in tests.