Skip to content

Instantly share code, notes, and snippets.

@joelkallman
Created July 30, 2013 15:45
Show Gist options
  • Save joelkallman/6114138 to your computer and use it in GitHub Desktop.
Save joelkallman/6114138 to your computer and use it in GitHub Desktop.
Dynamically Add Routes in Ember.Router
/**
* Add Route
*
* This will dynamically create the following routes for the name passed.
* Anywhere you see "name" below, replace with the passed param for name.
* e.g. addRoute(this, "posts") -> URL = "/posts", Route = PostsIndexRoute, etc.
*
* IMPORTANT: All templates render INTO "name" parent template. Put an {{outlet}} there
*
* URL | Route Name | Controller | Template
* ===============|================|=====================|================
* /name | NameRoute | NameController | name
* /name | NameIndexRoute | NameIndexController | `- name/index
* /name/add | NameAddRoute | NameAddController | `- name/add
* /name/23 | NameShowRoute | NameShowController | `- name/show
* /name/23/edit | NameEditRoute | NameEditController | `- name/edit
* ===============|================|=====================|================
*
* USAGE:
* App.Router.map(function () {
* addRoute(this, "posts");
* });
*
* For more info, go here:
* - http://emberjs.com/guides/concepts/naming-conventions/
* - http://emberjs.com/guides/routing/defining-your-routes/
*/
var addRoute = function (self, name) {
self.resource(name, function () {
this.route('add');
// NOTE: in the path, the name after the ":" (e.g. "item_id") will be used as the property name passed to the "model" method in the route.
this.route('show', { path: '/:item_id' });
this.route('edit', { path: '/:item_id/edit' });
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment