0

I have a route that has an afterModel hook.

afterModel: function(model, transition){
  transition.send('doInAppRoute');
}

I have an action in my application route:

doInAppRoute: function(){
  var controller = this.get('controller');
  controller.set('someProp', true);
}

When I allow the action to bubble from the route with the afterModel hook, I get the following error.

Error while processing route: embed Cannot read property 'set' of undefined TypeError: Cannot read property 'set' of undefined

If I put an action call to doInAppRoute in the application template, everything runs as expected.

If the action call to doInAppRoute bubbles, this.get('controller') in my application route is undefined. Why?

And how can this be changed so the bubbled action updates the application controller property?

user2517182
  • 1,241
  • 3
  • 15
  • 37
  • The controller will not be set up until AFTER all the model hooks have resolved. –  Sep 18 '16 at 18:56
  • I would say keep your state related properties in controller. In your case if you change `someProp` in route then it will not reflect in template unless it passes through setupController. – Ember Freak Sep 19 '16 at 06:12

1 Answers1

0

Thanks to @torazaburo for leading me in the right direction.

This is what I did.

Set someProp to an initial value in application route.

someProp: false,

Then in the the application route action do something like:

doInAppRoute: function(){
  this.set('someProp', true);
}

And then in the application route setupController do:

  setupController: function(controller, model){
    controller.set('someProp', this.get('someProp'));
    controller.set('model', model);
  }

Then everything should work.

user2517182
  • 1,241
  • 3
  • 15
  • 37
  • I suggest calling `this._super(...arguments);` instead of setting the model, it's less prone to bugs. – locks Sep 19 '16 at 07:20