How to get reference to current route from controller?
Question
I have a controller (KbRelatedGroupController
) that is loaded via a {{render}}
helper in a template.
In the controller's action, if I do this.get('target')
, it returns a reference to the parent controller for the page (KbShowController
).
If I call .target
on that, I get a reference to Discourse.Router
, which is no good to me.
What I want is a reference to a KbShowRoute
, and that is what I expected since .target
is supposed to produce the Route when called from a controller is it not?
Really confused here. Why is it so hard to get a reference to the current route from a controller?
Solution
The way I see it, you're not supposed to. You can let the action bubble up to the route:
App.KbShowRoute = Ember.Route.extend({
...
actions: {
something: function() {
console.log('called second');
}
}
});
App.KbShowController = Ember.Controller.extend({
...
actions: {
something: function() {
console.log('called first');
}
}
});
see docs
You could:
- Handle one part of the action in the controller, and let it bubble to the route by not returning anything in the controller's action handler
- Let the route handle the action (by not adding the action to your controller's action hash) and from the route use
this.controllerFor(this.routeName).sendAction('..', ...)
to call a different action (or part of the action) in the controller.
I hope this helps you!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow