Search⌘ K
AI Features

Moving the App Initialization Handler

Explore how to refactor a Marionette.js application by moving the app initialization handler from the index page into a dedicated app.js file. Understand when to use document ready calls and learn strategies for managing templates either on the server or via compiled approaches. This lesson helps you organize code into modular components, improving maintainability.

We'll cover the following...

Cleaning up our code

There’s not much left on our index page, but let’s finish cleaning up by moving our start handler code to our application file:

Node.js
ContactManager.on("start", function(){
ContactManager.ContactsApp.List.Controller.listContacts();
});

Our app.js file now contains the following code:

Node.js
var ContactManager = new Marionette.Application();
ContactManager.on("before:start", function(){
var RegionContainer = Marionette.LayoutView.extend({
el: "#app-container",
regions: {
main: "#main-region"
}
});
ContactManager.regions = new RegionContainer();
});
ContactManager.on("start", function(){
ContactManager.ContactsApp.List.Controller.listContacts();
});
...