Knockout view models can be reused across pages on a web site. Knockout components can be reused across view models and embedded in the view as a custom element.
See the Knockout Documentation
Here is the convention for component source using the example of the "Modal Confirm" component
<modal-confirm params="id:'confirm-save-modal',
headerText: 'Please confirm SAVE', bodyText: confirmText,
confirmClick: save"></modal-confirm>
See Modal Confirm for details about this component.
For a full example see the test page for modals:
To use a component, place the custom tag in your view. And load it in the init() funtion of the view model as in this example:
init(successFunction?: () => void) {
console.log('Init ModalTest');
let me = this;
me.application.registerComponents(['@pnut/modal-confirm'], () => {
me.bindDefaultSection();
successFunction();
});
}
Note that the component name is preceded by a code indication the source location.
In this case '@pnut' is a 'path token' expands to web.root/packages/knockout_view/pnut.
The viewmodel and html files would be found in 'component' and 'template'
subdirectories.
See View Model Intialization for information about other path tokens.
Creating a component is similar to creating a view model with some departures.
In the constructor of the component class, retrive and assign the attribute values. Example
constructor(params : any) {
let me = this;
if (!params) {
throw('Params not defined in modalConfirmComponent');
}
if (!params.name) {
throw('Parameter "name" is required.')
}
me.name(params.name);
}
Check out the existing components for detailed examples.