Batman.App
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.
-
Batman.currentApp
A Batman-wide reference to the currently running
Batman.App. -
@run() : App
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 thelayoutproperty on theApp. - Wait for the layout view to fire it's
readyevent. - Start the first action dispatch by telling the
Navigatorto begin monitoring the URL.
Note:
@runemits therunclass event on theApp, but not necessarily as soon as@runis called. Because thelayoutView renders asynchronously and may need to fetch other components, therunevent can and often does fire long after@runis called. If you need to execute code as soon as theApphas started running, add a listener to therunevent on theAppclass. If you need to execute code as soon as the layout has rendered, you can use thereadyevent on theAppclass.@runcan be called before or on theloadDOMEvent of the window.@runwill return the App if the commencement was successful and complete, orfalseif the App must wait for thelayoutto render or if theApphas already run.starting an application with DOMEvents
window.addEventListener 'load', -> MyApp.run()starting an application with jQuery
$ -> MyApp.run() - Instantiate a
-
@stop() : App
@stopstops theAppit'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. -
@routes
@routesis a class level property referencing the root levelNamedRouteQuerywhich allows for binding to routes on objects. Seedata-routebindings for more information. -
@controllers
@controllersis a class level property containing the singletonBatman.Controllerinstances for each subclass in the application. Thiscontrollersdirectory puts these instances at the lowercase name of the controller. For example, theTodosControllerwould be found atcontrollers.todos.@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" -
@layout
@layoutis the base view of the entire view hierarchy. By default, it will parse any data-* attributes in the entire document, excluding anydata-yield's, whenApp.run()is called. UseMyApp.layout = nullto disable the creation of this default view. -
@currentParams
@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:@currentParams.controller,@currentParams.action. -
@paramsManager
-
@paramsPusher
-
'run' class event
The
runclass event is fired once the app has run. This indeterminately but often happens before the app's layout has finished rendering. -
'ready' class event
The
readyclass event is fired once the app's layout is rendered.
