Skip to content

Instantly share code, notes, and snippets.

@Panman82
Last active January 14, 2016 22:09
Show Gist options
  • Save Panman82/7f27b083376e7447b3c4 to your computer and use it in GitHub Desktop.
Save Panman82/7f27b083376e7447b3c4 to your computer and use it in GitHub Desktop.
Adds an `activeRoutes` array to the Ember `Router`, along with a `routeFor` lookup method/helper
// app/initializers/router-active-routes.js
import Ember from 'ember';
export function initialize() {
Ember.Route.reopen({
// Helper function for Routes to lookup other routes,
// similar to how controllers can `controllerFor()`
routeFor( routeName ) {
// No route name passed? Nothing to return...
if (!routeName) {
return;
}
// Trying to get the current route, return it...
if ( routeName === this.get('routeName') ) {
return this;
}
// Lookup and return the route from the container
const container = Ember.getOwner( this );
return container.lookup(`route:${routeName}`);
}, // routeFor()
actions: {
_collectRoute( routes ) {
// Add the current route to the top of the stack
routes.unshift( this );
// Once at the "top", put the entire stack on the router
// Else continue up the route tree
if (this.get('routeName') === 'application') {
this.set('router.activeRoutes', routes);
} else {
return true;
}
} // _collectRoute()
} // :actions
}); // Ember.Route.reopen()
Ember.Router.reopen({
// Temporary placeholder
activeRoutes: [],
// Trigger the collection action
// on the farthest leaf Route
didTransition() {
this._super(...arguments);
this.send('_collectRoute', []);
} // didTransition()
}); // Ember.Router.reopen()
}
export default {
name: 'router-active-routes',
initialize
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment