Application controller

Contents

Application controller

The Application controller enables you to handle application lifecycle events.

main.js

Only one controller commonly used across all XP apps. This is known as the Application controller, or just "main.js".

To add an Application controller to your project, simply create the following file in your project: src/main/resources/main.js.

The main.js invoked when application start occurs. A common use of this controller is initialization and registration of event listeners.

main.js execution should complete as quickly as possible to prevent applications stalling. Don’t call remote endpoints or wait in loops for events inside main.js global scope.

Simple example:

// Log application started
log.info('Application ' + app.name + ' started');

Running code on stop:

// Log application started
log.info('Application ' + app.name + ' started');

// Log when application is stopped
__.disposer(function() {
    log.info('Application ' + app.name + ' stopped');
});

Contents