Access instance of new Ember Router
Asked Answered
A

3

10

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

Arrest answered 5/1, 2013 at 0:3 Comment(0)
U
35

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

Utmost answered 5/1, 2013 at 6:11 Comment(1)
And from route it is accessible as get(this, 'router')Extrude
E
1

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.

Elamitic answered 29/4, 2013 at 19:17 Comment(1)
Definitely a code smell. Instead, send an action and handle it in a route to transition.Gibbons
I
1

You can try this:

App.__container__.lookup('router:main').transitionTo('name_of_your_route');
Industrialist answered 22/8, 2013 at 16:40 Comment(1)
This is OK to do for debugging but should be avoided in production code.Gibbons

© 2022 - 2024 — McMap. All rights reserved.