Skip to content

Instantly share code, notes, and snippets.

@dunglas
Last active December 10, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dunglas/4412016 to your computer and use it in GitHub Desktop.
Save dunglas/4412016 to your computer and use it in GitHub Desktop.
Use a root option for the router
# Part of the Dunglas' Symfony 2 / Chaplin.js tutorial
# http://dunglas.fr/2012/12/utiliser-chaplin-js-et-backbone-js-avec-symfony-2-installation-et-configuration/
define [
'chaplin'
'views/layout'
'routes'
], (Chaplin, Layout, routes) ->
'use strict'
# The application object
# Choose a meaningful name for your application
class HelloWorldApplication extends Chaplin.Application
constructor: (@root) ->
# Set your application name here so the document title is set to
# “Controller title – Site title” (see Layout#adjustTitle)
title: 'Chaplin Example Application'
initialize: ->
super
#console.debug 'HelloWorldApplication#initialize'
# Initialize core components
@initDispatcher()
@initLayout()
@initMediator()
# Application-specific scaffold
@initControllers()
# Register all routes and start routing
@initRouter routes, pushState: true, root: @root
# You might pass Router/History options as the second parameter.
# Chaplin enables pushState per default and Backbone uses / as
# the root per default. You might change that in the options
# if necessary:
# @initRouter routes, pushState: false, root: '/subdir/'
# Freeze the application instance to prevent further changes
Object.freeze? this
# Override standard layout initializer
# ------------------------------------
initLayout: ->
# Use an application-specific Layout class. Currently this adds
# no features to the standard Chaplin Layout, it’s an empty placeholder.
@layout = new Layout {@title}
# Instantiate common controllers
# ------------------------------
initControllers: ->
# These controllers are active during the whole application runtime.
# You don’t need to instantiate all controllers here, only special
# controllers which do not to respond to routes. They may govern models
# and views which are needed the whole time, for example header, footer
# or navigation views.
# e.g. new NavigationController()
# Create additional mediator properties
# -------------------------------------
initMediator: ->
# Create a user property
Chaplin.mediator.user = null
# Add additional application-specific properties and methods
# Seal the mediator
Chaplin.mediator.seal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment