chaplinjs/chaplin — pull request #467 by iangreenleaf (2013-03-07 23:44:21+00:00)
Title: Don't route requests immediately after loading routes

We ran into trouble trying to use the "url" view helper in Brunch for named routes in our layout. This helper depends on the router (obviously), but the layout is being initialized in `@initLayout()`, which comes before `@initRouter` in the initialization sequence.

I think the best solution is to load routes at the beginning of application init, making them available to the rest of the init process. Then we call `startHistory()` at the end to actually route requests.

This will require an adjustment to the Chaplin init sequence. With this patch, our `application.coffee` file in Brunch now looks like this:

```coffeescript
initialize: ->
super

# Register all routes
@initRouter routes

# Initialize core components
@initDispatcher controllerSuffix: '-controller'
@initLayout()
@initMediator()

# Application-specific scaffold
@initControllers()

# Actually start routing
@router.startHistory()

# Freeze the application instance to prevent further changes
Object.freeze? this
```