Skip to content

v1 05. Application Loader

Frank Hossfeld edited this page Nov 28, 2019 · 1 revision

Application Loader

Nalu provides a mechanism to execute asynchronous actions during application start.

This is a good place, to

  • get informations from the server and store them f. e.: in the context or a cache for later use

  • load application wide style sheets and provide them via the application context

  • do anything else, you want to do at application start ...

Once all asynchronous action(s) are executed, the control can be return to the Nalu framework by calling finishLoadCommand.finishLoading();.

Creating a application loader

To use a application loader it is necessary to implement a class that extends AbstractApplicationLoader. This abstract class requires a method that have to be implemented:

  • load: this method will be called during application start and before the initial route is called. When all loadings are done, the method finishLoadCommand.finishLoading(); has to be called to return the control to Nalu and continue the application start process.

Here is an example of an application loader:

public class MyApplicationLoader
  extends AbstractApplicationLoader<MyContext> {

  @Override
  public void load(FinishLoadCommand finishLoadCommand) {

    // do the application loading

    finishLoadCommand.finishLoading();
  }
}

If there are more than one server call to be done inside the application loader, you should take a look at sema4g.

A loader has a reference to the instances of:

  • the event bus
  • the application context
  • the router (since v1.2.1)

Important:

Do not use the router inside the application loader to route. Because the loader is called during the start phase of a Nalu application, some things might not be set correctly. Use the router instance only to inject in some fo your application classes for later use.

Setting a loader

To set a loader for an application, you need to add your loader to the @Application annotation and set the value of the loader attribute to your loader class.

@Application(loader = MyApplicationLoader.class,
             startRoute = "/applicationShell/application/search",
             context = MyApplicationContext.class,
             routeError = "/errorShell/error")
interface MyApplication
  extends IsApplication {
}

Handling application loader failure

In case a loading error occurs that prevent the application from starting, it is a good practice to inform the user about the error using a popup (or something else) and route the application to a technical error page.