13

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?

Mohan Kumar
  • 621
  • 1
  • 8
  • 25

1 Answers1

37

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");
    }
  }
});
artych
  • 3,669
  • 1
  • 17
  • 30
  • 4
    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? – Roger Mar 07 '16 at 09:16
  • 1
    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. – Scott Fanetti May 19 '17 at 13:15