How does one access the instance of the new Ember router? The API docs seem to be refer to the old router or are incorrect: http://emberjs.com/api/classes/Ember.Router.html
RouterV2 is not easily accessed via a global constant, making it more difficult to do things the 'wrong' way. The main thing to keep in mind is that you should not be accessing the router (or anything else) via a global variable. Doing so is a generally bad practice, leading to code that is very hard to test. Unfortunately with the old router it was pretty easy to do something like App.router.transitionTo('whatever')
- you can find examples of that all over the place, but it's not a good idea.
Instead of working with a global reference, ember injects local references to the router in just those places where it is needed.
- From a model: Inaccessible. Models should not be talking to the router
- From a controller:
router = this.get('target')
- From a view: View should not access the router, but events it sends to the controller will bubble up. For example:
this.get('controller').send('search', term)
- From a template: Use the
{{action}}
or{{#linkTo}}
helpers to send events (via the controller) or transition to another url.
For more detail, see the notes on this commit: https://github.com/emberjs/ember.js/commit/5becdc4467573f80a5c5dbb51d97c6b9239714a8
** Update **
I put together a lightning talk on with more detail on the new router for the January EmberNYC meetup - slides are here: How I learned to stop worrying and love the router
I believe this works all over the place:
App.Router.router.transitionTo('home')
I use it in a view. I'm aware there is a bit of a code smell here. However, it works.
You can try this:
App.__container__.lookup('router:main').transitionTo('name_of_your_route');
© 2022 - 2024 — McMap. All rights reserved.
get(this, 'router')
– Extrude