Callum Linington
Callum Linington

Reputation: 14417

Aurelia hook into router before app.js

So the aurelia app gets bootstrapped from the main.js

bootstrap(function(aurelia) {
  aurelia.use
    .standardConfiguration()
    .developmentLogging();

  aurelia.start().then(() => aurelia.setRoot('app', document.body));
});

The source code tells me there is a router() method you can call on the FrameworkConfiguration which is what aurelia.use gives you.

But the implementation gives you no hooks, it just includes the framework-templating-router.

The reason I want a hook in, is because I want to do an api call in the activate() of the app.js however, I wish to slide in a Navigation Pipeline middleware before the api call is made.

configureRouter is called after activate() which is obvious. This means I can dynamically inject menu items (api behind authorised walls) to build up my initial screen. If I get a 401 it rejects the activate() promise - which I want - however what would be better is if the pipeline step could Redirect('login').

If I were to put custom login the activate() then I would have two places of redirection, but also it just doesn't fit into the aurelia design very well.

There is an alternative solution which is to defer configuring the router till a child view of the app.js but it doesn't seem as nice.

Upvotes: 1

Views: 439

Answers (1)

user677526
user677526

Reputation:

In your app.js, you can do this:

import {Router} from 'aurelia-router';

@inject(Router)
export class App {
    constructor (router) {
        this.router = router;
        this.router.configure(config => ...);
    }

    activate () {
        ...
    }
}

Essentially....you don't have to use the configureRouter method. It's just there as a convenience. Using the constructor allows more flexibility in this case.

Upvotes: 1

Related Questions