Take an Ember.js router like so:
App.Router.map ->
@route 'login'
@route 'account', path: '/:handle'
App.AccountRoute = Ember.Route.extend
model: (params) ->
App.Account.findByHandle(params.handle)
serialize: (model) ->
{handle: model.get('handle')}
Assuming the handle bschaeffer
exists, entering the application at app.com/bschaeffer
will
find the Account and load the template for the route into the application template. However,
entering the application at app.com/invalid_handle
will cause the model to be rejected and
prevent from Ember from rendering the application template, in turn preventing you from rendering
a "Not Found" message into the application template. I did't like this because I wanted the
application template to be there, so heres how we handle that:
App.AccountRoute = Ember.Route.extend
model: (params) ->
App.resolveAlways ->
App.Account.findByHandle(params.handle)
renderTemplate: (controller, account) ->
if customIsErrorChecker(account)
# Render a 404 page
@render('notFound')
else
# Business as usual
@_super()
- Entering a route means loading the application for the first time at the given route.
- If you don't care about having the application template, none of this matters.
- You can render the application template in an error event handler, but that won't work if you try to re-render the application template again after some successful route states.