edance
edance

Reputation: 620

Nothing handles action on immediate event - Ember

Alright so I am running into this issue, check it out

http://jsbin.com/rarubesuxutu/1/edit?html,css,js

I am trying to send an event right after init but the event does not get handled. Instead the action just errors out. Why does Ember handle it like this? When is it ok to send an event? Is there some callback so I can set my observers?

Thanks!

Upvotes: 2

Views: 96

Answers (2)

edance
edance

Reputation: 620

Ok so I figured it out. I had a misunderstanding of the order of the callbacks.

Here is my updated version. While it's not fixed it helps understand the order of the route and controller initializations. The IndexController init is called before the routes setupController. This triggers the event before the route was set.

Upvotes: 3

abuani
abuani

Reputation: 180

Okay, so I'm not sure why you want to send an action right after init, as you should be able to define all of your variables in the setupController or in the controllers init phase, so I'll show you how I would go about it. JSBIN

Effectively what you can do is on init for the controller, you can do this:

theTruth: false,
setTheTruth: function(){
      this.set('theTruth', true);
}.on('init'),

Then you can have an observer that watches for changes on this variable like so:

truthChanged:function(){
    console.log("TRUTH HAS CHANGED");
}.observes('this.theTruth'),

Ideally though, you can forgo all of this by taking advantage of the routes setupController function like soJSBIN

setupController: function(controller, model) {
    controller.set('model', model);
    controller.set('theTruth', true);
},

This way you don't even deal with handling anything in the init event. However, say you have a requirement that makes you have to do anything with the init event, you should separate your observer from the .on('init') event, and let them coexist separately. Let me know if this works for you.

Upvotes: 0

Related Questions