:metal: KnockoutJS Goodies Monorepo
Works with router.plugins.component to declaratively initialize a viewModel before rendering.
ctx.component.viewModel
)INITIALIZED
Symbolimport {
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 */
}
}
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)