layout | title | module_path |
---|---|---|
default |
Chaplin.Application |
src/chaplin/application.coffee |
The Chaplin.Application object is a bootstrapper and a point of extension
for the core modules of Chaplin: the Dispatcher, the
Layout, the Router, and the
Composer. The object is inteded to be extended by your
application. The initialize
method of your derived class must initialize
the core modules by calling the initRouter
, initDispatcher
, initLayout
,
and then launching navigation with startRouting
Chaplin = require 'chaplin'
routes = require 'routes'
module.exports = class Application extends Chaplin.Application
initialize: ->
# No need to call super as the base class method is a no-op.
# Initialize core components in the required order.
@initRouter routes
@initDispatcher()
@initComposer()
@initLayout()
# Actually start routing.
@startRouting()
var Chaplin = require('chaplin');
var routes = require('routes');
var Application = Chaplin.Application.extend({
initialize: function() {
// Initialize core components in the required order.
this.initRouter(routes);
this.initDispatcher();
this.initComposer();
this.initLayout();
// Actually start routing.
this.startRouting();
}
});
module.exports = Application;
# [...]
class Application extends Chaplin.Application
# [...]
title: "Fruit"
mediator.publish '!adjustTitle', 'Apple'
# Document title is now "Apple — Fruit".
// [...]
var Application = Chaplin.Application.extend({
// [...]
title: 'Fruit'
});
mediator.publish('!adjustTitle', 'Apple');
// Document title is now "Apple — Fruit".
To replace the dispatcher with a derived class (possibly with various extensions), you'd override the initDispatcher
method and construct the dispatcher class as follows:
# [...]
Dispatcher = require 'dispatcher'
class Application extends Chaplin.Application
# [...]
initDispatcher: (options) ->
@dispatcher = new Dispatcher options
// [...]
var Dispatcher = require('dispatcher');
var Application = Chaplin.Application.extend({
// [...]
initDispatcher: function(options) {
this.dispatcher = new Dispatcher(options);
}
});
- routes
The routing function that contains the match invocations, normally located in
routes.coffee
.
To replace the router with a derived class (possibly with various extensions), you'd override the initRouter
method and construct the router class as follows (ensuring to start the routing process as well):
# [...]
Router = require 'router'
class Application extends Chaplin.Application
# [...]
initRouter: (routes, options) ->
@router = new Router options
# Register any provided routes.
routes? @router.match
// [...]
var Router = require('router');
var Application = Chaplin.Application.extend({
// [...]
initRouter: function(routes, options) {
this.router = new Router(options);
// Register any provided routes.
if (routes != null) routes(this.router.match);
}
});
To replace the layout with a derived class (possibly with various extensions), you'd override the initComposer
method and construct the composer class as follows:
# [...]
Composer = require 'composer'
class Application extends Chaplin.Application
# [...]
initComposer: (options) ->
@composer = new Composer options
// [...]
var Composer = require('composer');
var Application = Chaplin.Application.extend({
// [...]
initComposer: function(options) {
this.composer = new Composer(options);
}
});
To replace the layout with a derived class (possibly with various extensions), you'd override the initLayout
method and construct the layout class as follows:
# [...]
_ = require 'underscore'
Layout = require 'layout'
class Application extends Chaplin.Application
# [...]
initLayout: (options) ->
@layout = new Layout _.defaults options, {@title}
// [...]
var _ = require('underscore');
var Layout = require('layout');
var Application = Chaplin.Application.extend({
// [...]
initLayout: function(options) {
this.layout = new Layout(_.defaults(options, {title: this.title}));
}
});