Skip to content

Instantly share code, notes, and snippets.

@jordanyaker
Last active December 13, 2015 19:48
Show Gist options
  • Save jordanyaker/4965340 to your computer and use it in GitHub Desktop.
Save jordanyaker/4965340 to your computer and use it in GitHub Desktop.

So, at the end of the day, there are a lot of lessons that are taken as implicit in the world of Ember.js. However, much like everything else in Ember, these items aren't so easily understood.

Controllers Are Control Bindings

This is an important lesson that I keep forgetting. Controllers are NOT like controllers in a Server-side MVC. Controllers are NOT like the Collections that you find in Backbone.js. They are something that presents the model to the view. That's it. For example, given the following configuration snippet:

App.Router = Em.Router.map ->
  @resource 'root', { path: '/' }, ->
    @route 'create'

There are feasibly going to be four different views (i.e., ApplicationView, RootView, RootIndexView, and RootCreateView) that are implicityly created by the system. Each of these views will have their respectful controller setup (i.e., ApplicationController, RootController, RootIndexController, RootCreateController). If you want to setup the objects that are being served by the controller to the view, then the controllerFor method will allow for good manipulation of the controller from anywhere (anywhere that it's accessible). I can't reiterate how important this lesson is. I feel so stupid for this not having "clicked" in my head earlier.

Hijacking The Process

For a given route, it is possible for hijacking the entire handling of that route from all of the implicit auto-magics that are part of ember. In any given route handler, all you need to do is implement the renderTemplate, model, and/or the setupController method. These methods are callsed in the following order for a given route:

  1. model
  2. setupController
  3. renderTemplate

Controlling this process will allow you to control everything about a given route's handling.

Making Views Look How You Want

A further discovery involved finding the following tip in the source file:

App.PostRoute = App.Route.extend
  renderTemplate: ->
    this.render 'myPost',     // the template to render
      into: 'index',          // the template to render into
      outlet: 'detail',       // the name of the outlet in that template
      controller: 'blogPost'  // the controller to use for the template

This little piece says it all. You can tell the route handler which template to render, where to render it, what outlet to put it in to, and, most importantly, you can wire it up to a controller. The controller could be a lookup key, or it could be an instance of a common controller (i.e., "controller: @controllerFor('blogPost')"). It will work either way.

That's it for now. This gist will be updated as I find more things to remember.

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