Usage
Syntax
Subject to change!
The current syntax is experimental and subject to change. It differs from the syntax used by the server-side renderer.
The linter utilizes HTML comments to convey information. These comments are referred to as "directives".
Directives
Possible Confusion
The syntax is similar to knockout binding handler comments. Notice the dash between "ko" and the directive. Knockout comments always ha
<!-- ko-directive ... -->
ko-import
- It's syntax is similar to ES modules. These imports can be utilized in other directives or binding handlers.ko-viewmodel
- Sets the viewmodel for the current view.
Viewmodels
To import the view model, use the ko-import directive
like the below example.
<!-- ko-import ViewModel from './viewmodel' -->
Then, set the view model for the current file using the ko-viewmodel directive:
<!-- ko-viewmodel ViewModel -->
TIP
You can pass typeof ViewModel
to the ko-viewmodel
directive when using instances (singleton).
Binding Handlers
Use the ko-import
directive to import the binding handler type.
<!-- ko-import custom from './binding-handler' -->
Then, you can use the binding handler.
<div data-bind="custom: ..."></div>
Declaring Binding Handlers
You can export an existing implementation of ko.BindingHandler
or declare it in a seperate declaration typescript (.d.ts
) file like shown below.
declare class CustomBindingHandler implements ko.BindingHandler<...> { ... }
export default CustomBindingHandler;
The generic paramater T
passed to ko.BindingHandler
tells the linter what type is expected.
ko.BindingHandler<string>;
Transforming Child Context
In the rare event the binding handler creates a new child context, the binding should declare the method transformContext
. No implementation is required.
declare class CustomBindingHandler implements ko.BindingHandler<...> {
transformContext!: (input: InputType, context: ParentContextType) => ChildContextType
}