Last active
January 14, 2016 22:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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