v0.8

AppOne

The one route i.e. one controller app. Use this class to start an application that doesn't require routing, such as a browser extension.

class AppOne → AppOne.js

Instance

To start the app create an object instance.
const appOne = new AppOne(appName);

Properties

The appOne instance properties are:
Property Description Type Default
$appName The application name. It defines the window property. For example if $appName='myApp' then window.myApp string dodoApp
ctrls A collection of all controllers. This makes possible to use a controller's methods inside another controller. object {}
ctrlConstants A controller constants which will not be deleted when one controller is replaced by another controller (when route is changed).
{$appName, $fridge, $httpClient, $auth, $debugOpts, $model, $modeler, $dd}
object {...}
$preflight array of preflight functions, will be executed on every route before the controller's __loader() Function[] []
$postflight aarray of postflight functions, will be executed on every route ater the controller's __postrend() Function[] []
$debugOpts Debugger options. Set specific option to true if you want to see the debugging messages. See all options. object {...}

Methods

fridge(name, val) :App

Set the subproperty of the controller's $fridge property.
    ARGUMENTS:
  • name :string - $fridge property name, for example if name is 'api_url' then the frozen constant will be this.$fridge.api_url and it can be used in every controller
  • val :any - the fridge property value

httpClient($httpClient) :App

Set the $httpClient property. It can be called with this.$httpClient from the controller.
The HTTP client is used for certain API calls.
    ARGUMENTS:
  • $httpClient :object - the http client object
EXAMPLE:
An app needs HTTP client in the controller to fetch the JSON from API.

// src/const/$httpClient.js
=========================
import { corelib } from '@mikosoft/dodo';

// default HTTP client
const opts = {
  encodeURI: true,
  timeout: 21000,
  responseType: '', // 'blob' for file download (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType)
  retry: 0,
  retryDelay: 1300,
  maxRedirects: 0,
  headers: {}
};
const $httpClient = new corelib.HTTPClient(opts);

export { $httpClient };


// src/app.js
=========================
import { $httpClient } from './conf/httpClient.js';
const appOne = new AppOne('myApp');
appOne.httpClient($httpClient);
Now this.$httpClient is injected in every controller and can be used to call the API.
// src/controllers/SomeCtrl.js
=============================
import { Controller } from '@mikosoft/dodo';

class SomeCtrl extends Controller {
  async __init() {
    const answer = await this.$httpClient.askJSON('https://jsonplaceholder.typicode.com/posts/1');
  }
}

auth($auth) :App

Inject the auth library in the controller and use it as this.$auth .
    ARGUMENTS:
  • $auth :object - instance of the Auth class

debug($debugOpts) :App

Set the debug options.
EXAMPLE:
Lets define a constant which will be needed in all or most of the controllers, for example an API URL.
import { $httpClient, $debugOpts } from './conf/index.js';
const appOne = new AppOne('myApp');
appOne
  .httpClient($httpClient)
  .debug($debugOpts);

async i18n(i18n) :void

Set the global, window i18n property.
Language object can be loaded from database, files, browser storage or some other sources.
If input argument "i18n" is undefined the Vite Glob Import will be used by default and language files from src/i18n/ will be loaded.
    ARGUMENTS:
  • i18n :object - object with language translations, for example {de: {common: {USERNAME: 'Nutzername'}, home: {TITLE: 'Startseite', LOGIN: 'Anmeldung'}}}
EXAMPLE:
As the i18n() is async function it must be wrapped in async function so await can be used. Call i18n() before routes() and listen() because language json files must be read before controller execution.
const initApp = async () => {
  const appOne = new AppOne('dodoApp');

  appOne
    .i18n(); // load language JSON files in the window.dodoApp.i18n
    .auth($auth)
    .httpClient($httpClient)
    .debug($debugOpts);
};

initApp().catch(console.error);