Batman.App is the central object in any Batman application. It manages the routing table and the current URL parameters, as well as the initial start of the application. It should also be used as a namespace for models and views so that other objects can find them. Batman assumes that there will only ever be one
Batman.App in use at once.
A Batman-wide reference to the currently running
App.runis the central entry point for a Batman application.
@runtakes these steps to initialize a Batman application:
- Instantiate a
Dispatcher, an internal object for mananging action dispatch to controllers.
- Instantiate a
Navigator, an internal object for managing the URL via pushState or hashbangs.
- Instantiate the
layoutview according to the
layoutproperty on the
- Wait for the layout view to fire it's
- Start the first action dispatch by telling the
Navigatorto begin monitoring the URL.
runclass event on the
App, but not necessarily as soon as
@runis called. Because the
layoutView renders asynchronously and may need to fetch other components, the
runevent can and often does fire long after
@runis called. If you need to execute code as soon as the
Apphas started running, add a listener to the
runevent on the
Appclass. If you need to execute code as soon as the layout has rendered, you can use the
readyevent on the
@runcan be called before or on the
loadDOMEvent of the window.
@runwill return the App if the commencement was successful and complete, or
falseif the App must wait for the
layoutto render or if the
Apphas already run.
starting an application with DOMEvents
window.addEventListener 'load', -> MyApp.run()
starting an application with jQuery
$ -> MyApp.run()
- Instantiate a
Appit's called upon. The URL will stop being monitored and no more actions will be dispatched. In usual Batman development you shouldn't have to call this.
@routesis a class level property referencing the root level
NamedRouteQuerywhich allows for binding to routes on objects. See
data-routebindings for more information.
@controllersis a class level property containing the singleton
Batman.Controllerinstances for each subclass in the application. This
controllersdirectory puts these instances at the lowercase name of the controller. For example, the
TodosControllerwould be found at
@controllersideally should never be bound to, but it is very useful for debugging in the console and other such workarounds.
test "App.controllers references a directory of controller instances", -> class Alfred extends Batman.App class Alfred.TodosController extends Batman.Controller controller = Alfred.get('controllers.todos') equal Batman._functionName(controller.constructor), "TodosController"
@layoutis the base view of the entire view hierarchy. By default, it will parse any data-* attributes in the entire document, excluding any
App.run()is called. Use
MyApp.layout = nullto disable the creation of this default view.
@currentParamscontains the URL parameters for the current request, including the path relative to the app's base path (
Batman.config.pathToApp). Some interesting parts to look at:
runclass event is fired once the app has run. This indeterminately but often happens before the app's layout has finished rendering.
readyclass event is fired once the app's layout is rendered.