Skip to content
Brod edited this page Jul 15, 2018 · 13 revisions

The Jagwah class is used to create a new application instance, although you can create multiple instances within a single Browser context it's not the intended usage and has not been thoroughly tested.

Usage

You can create a new Jagwah instance using the Jagwah() class.

const jagwah = new Jagwah(...);

Options

When creating a new Jagwah instance you can pass in a Jagwah.options object.

const jagwah = new Jagwah({
  constants: { ... },
  providers: [ ... ],
  templates: [ ... ],
  routes: [ ... ],
});

Constants

The constants property is good for "global" information that you might need throughout your application, such as environment variables, urls and more. If the constants property is used it should be an object with any shape.

const jagwah = new Jagwah({
  constants: {
    CDN_URL: 'https://cdn.jagwah.com/',
    API_URL: 'https://api.jagwah.com/',
  },
});

Providers, Templates, Routes

The providers, templates and routes properties can be used to register arrays of Providers, Templates and Routes instead of using the individual methods (jagwah.Provider(), jagwah.Template(), jagwah.Route()).

const jagwah = new Jagwah({
  providers: [MyProvider, ...],
  templates: [MyTemplate, ...],
  routes: [MyRoute, ...],
});

Tip

An neat way to use the providers, templates and routes properties is to use barrel files and import your modules all at once and then use the Object.values() method.

import * as providers from './providers';

const jagwah = new Jagwah({
  providers: Object.values(providers),
  ...
});

Start

The start() method is used to initialize a Jagwah application, it takes one argument (Jagwah.start.options) which has optional before and after properties. Those properties accept an array of middleware-like classes, you can use dependency injection and run tasks before the first update occurs.

The example below has a single before handler injects an AccountProvider to check the sign-in status before the first render. Things that occur within the before handler should be fast because they'll prevent the initial render.

export class BeforeStartCheckAuth {
  constructor(
    @Inject('$account') private $account: providers.AccountProvider,
  ) {}
  async task() {
    await this.$account.signCheck();
  }
}

jagwah.start({
  before: [BeforeStartCheckAuth],
  after: []
});

The classes should use dependency injection during construction and expose a task() method which will be called by Jagwah.

Events

During runtime Jagwah will emit events via the radio property which works as a simple event emitter.

jagwah:start

Called after jagwah.start() methods completes.

jagwah:template:register

Called after jagwah.Template() methods completes, if the Template was named it will return the name, otherwise it will return "anonymous".

jagwah:provider:register

Called after jagwah.Provider() methods completes, it will return the name of the Provider.

jagwah:route:register

Called after jagwah.Route() methods completes, it will return the path of the Route.

jagwah:update

Called after jagwah.update() methods completes.

jagwah:router:update:before

Called before a route change occurs.

jagwah:router:update:after

Called after a route change occurs.