Skip to content

Instantly share code, notes, and snippets.

@huafu
Created August 10, 2013 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save huafu/6199311 to your computer and use it in GitHub Desktop.
Save huafu/6199311 to your computer and use it in GitHub Desktop.
Ember router, routes, controllers and dependencies
App = Em.Application.create()
App.Router.map ->
@resource 'organizations', path: 'orgs', ->
@resource 'organization', path: ':organization_id', ->
@route 'index', path: '/'
@route 'settings'
# the route for all organizations
App.OrganizationsRoute = Em.Route.extend
# not sure but if you need to load all the organizations in the OrganizationsController:
model: (params) ->
App.Organizations.find()
# the associated controller (it has to be an ArrayController if we want to load all models in it)
App.OrganizationsController = Em.ArrayController.extend
# we wanna store the currently selected organization there (when the user goes to an organization route)
selectedModel: null
# now the singular one (second level)
App.OrganizationRoute = Em.Route.extend
# this is the default behavior for Ember data, so we just call the method of inherited class,
# but we set the selected model of the organizationS controller
model: (params) ->
@_super arguments... # this will do App.Organization.find(params.organization_id) because we named the dynamic segment organization_id
@controllerFor('organizations').set 'selectedModel', model
# The controller for Organization
# its content will be set to the current Organization model
# We don't even need to declare it, if so Ember will do for us
App.OrganizationController = Em.ObjectController.extend()
# the interesting part. Because I'm a controller used by a route INSIDE the organzation route
# which has its model in the organization controller, I can depends on this controller to get its model
# instead of duplicating it
App.OrganizationSettingsController = Em.Controller.extend
# this is how to tell ember that we depend on some parent controllers
needs: ['organization']
# I can do some binding to access easily my organization, else a @get('controllers.organization.content') would do it
# even without the .content at the end
organization: null # good to define a property to null before defining a binding on it
organizationBinding: 'controllers.organization
@huafu
Copy link
Author

huafu commented Aug 10, 2013

Also know that any resource has its ResourceIndexRoute and associated ResourceIndexController even if no indexroute is defined, corresponding to the path /. This except if a resource is defined without function: @resource 'stuff'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment