Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access instance of new Ember Router

Tags:

ember.js

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

like image 632
bcardarella Avatar asked Jan 05 '13 00:01

bcardarella


1 Answers

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

like image 113
Mike Grassotti Avatar answered Nov 13 '22 23:11

Mike Grassotti