knockout-contrib

:metal: KnockoutJS Goodies Monorepo

View the Project on GitHub Profiscience/knockout-contrib

router.plugins.init

Version Dependency Status Peer Dependency Status Dev Dependency Status Downloads

Works with router.plugins.component to declaratively initialize a viewModel before rendering.

Usage

import {
  Route,
  componentRoutePlugin,
  componentInitializerRoutePlugin,
  INITIALIZED,
} from '@profiscience/knockout-contrib'

Route.usePlugin(componentRoutePlugin).usePlugin(componentInitializerRoutePlugin) // **MUST** come after component plugin

class DataModel {
  public [INITIALIZED] = this.init()

  public async init() {
    /* do some async init stuff */
  }
}

class ViewModel {
  /**
   * Attach directly to the viewModel...
   */
  public [INITIALIZED] = this.init()

  /**
   * ...or any enumerable property
   */
  public data = new DataModel()

  async init() {
    /* do some async init stuff */
  }
}

What’s with the symbol?

Prevents naming conflicts. The alternative is to use a “magic” property, like initialized, but that would a) prevent you from using your own initialized property/method in your viewModel (which is something you’ll probably want at one point or another) b) make it difficult for developers unaware of the use of this plugin to track down issues or figure out where the magic is happening (explicit imports lead to discoverability)