@profiscience/knockout-contrib-model
v1.0.0-alpha.45
Published
An opinionated model with first-class TypeScript support and router integration with KnockoutJS
Downloads
13
Readme
@profiscience/knockout-contrib-model
NOTE: Assumes a rudimentary understanding of KnockoutJS and Webpack (or the bundler du jour).
Composable view and data model builders for KnockoutJS with @profiscience/knockout-contrib-router integration designed with the following goals in mind...
User Experience
No Jarring
There are two main schools of thought on loaders and SPA navigation. The first is is to allow each view/component (and subview in nested routing) to manage itself. This yields a UX similar to Facebook, where parts of the page come in at different times. There is nothing wrong with this approach, but it requires more code, and more care to be taken in constraining dimensions in CSS so that the page doesn't jar — in many cases it's impossible to prevent jarring.
The alternative is the approach that is used here, which is to display a global loader and show that until the next page is completely ready, and then render it. No view is rendered until all of its data has been loaded.
GitHub uses this approach, and IMHO it is an optimal UX.
Lazy Loaded
Minimal code should be required for initial page load, as well as navigation.
Developer Experience
First-Class TypeScript Support
When following best-practices, types should be intuited as strictly as possible — i.e. noImplicitAny
compatibility. TypeScript usage is henceforth assumed.
Minimal Logic Boilerplate
Boilerplate should be declarative and mostly confined to defining types.
Loading async data should require no extra effort additional code. i.e. there should be no need to wrap component templates with if: ready
and create custom CSS
to prevent jarring. Nor should a developer have to create middleware by hand for each route.
Reusability
Heed to DRY principles. Prevent repetition wherever possible.
Testability
Unit testing views should be trivial. TDD should help a developer, not hurt.
Extensibility
Easily allow extending, taking care to avoid conflicts and prevent leaks by using Symbols where applicable.
API
@TODO
import { DataModelConstructorBuilder, ViewModelConstructorBuilder } from '@profiscience/knockout-contrib-model'
Usage
app.js
import * as ko from 'knockout'
import { Route, Router } from '@profiscience/knockout-contrib-router'
import { componentPlugin, dataPlugin } from '@profiscience/knockout-contrib-router-plugins'
const home = new Route('/', {
// via router.plugins.component package
component: () => ({
template: import('./template.html'),
viewModel: import('./viewModel')
})
})
Router
.useRoutes(home)
Route
// ** REQUIRED **
.usePlugin(componentPlugin)
// Must be registered ** AFTER ** component plugin. Delays render until all
// DataModel properties on the viewModel instance have been initialized.
.usePlugin(dataPlugin)
ko.applyBindings()
viewModel.js
import { ViewModelConstructorBuilder } from '@profiscience/knockout-contrib-model'
import { DataModel } from './dataModel'
export default class ViewModel extends ViewModelConstructorBuilder {
public data = new DataModel()
}
dataModel.js
import { DataModelConstructorBuilder } from '@profiscience/knockout-contrib-model'
export interface IDataModelParams {}
export default class DataModel extends DataModelConstructorBuilder<IDataModelParams{
public name: string
public async fetch() {
// assume this returns { name: 'Casey' }, as defined above
return await $.get('https://example.com/api/name')
}
}
template.html
Hello, <span data-bind="text: data.name"></span>
Walking through this code step-by-step, the following occurs...
- Page is loaded on the home url (
/
) - Router loads the component files (
viewModel.js
andtemplate.js
) - componentPlugin creates a viewModel instance and registers component with
{ viewModel: { instance } }
- dataPlugin looks for DataModel instances on the viewModel instance; if found, prevents render until initialized
- Router completes render