ko-typed provides observable extensions for restricting and converting observable values based on type. Supports validation.
ko extenders are used to wrap a given observable and provide a typed interface.
ko.typed.options
Default options used for each extender.
Observables extended using type
or convert
contain the following API.
-
observable.typeName : Undefined|String
Undefined
The underlying observable has a custom or undefined type.String
|
seperated string of type names. e.g.'Undefined|Number|String
-
observable.typeName : Array
An array of type names supported byobservable
. Note that this is just a guide as the observable may have further restrictions. -
observable.typeCheck: Function
Syntaxcheck(value) : Boolean
A method which returnstrue
if the providedvalue
is a supported type. -
observable.typeChecks: Object.Literal
A map of type checks functions keyed on TypeName. Each type check has the syntaxcheck(value) : Boolean
. Each check function returns true if the provided value is of the correct type and is supported. -
observable.readError: ko.observable
A simple observable containing the (caught) error produced from the last read operation. Containsundefined
if no error was generated during the last read operation. -
observable.writeError: ko.observable
A simple observable containing the (caught) error produced from the last write operation. Containsundefined
if no error was generated during the last write operation.
The following options can be applied at any level. Global default (ko.typed.options
), extender default (ko.extenders.<EXTENDER>.options
),
and individual extensions (ko.observable().extend({ <EXTENDER>: options })
).
-
options.validation
Object literal containing default validation options.-
options.validation.enable: Boolean
Defaultfalse
. If true, extensions enable and contain validation using the included validation library.ko.validation
must be defined at the time of extension. -
options.validation.read: Boolean
Defaulttrue
. If true, validation is performed when reading the resulting observable. -
options.validation.write: Boolean
Defaulttrue
. If true, validation is performed when writing the resulting observable. -
options.validation.target: Boolean
Defaultfalse
. If true, validation is attached to the underlying observable. -
options.validation.result: Boolean Default
true`. If true, validation is attached to the resulting observable. -
options.validation.message: Undefined|String
Defaultundefined
.Undefined
Validation error message is the message from the exception that caused validation to fail.String
This message is used for validation errors.
-
-
options.exRead
Object literal containing options for handling exceptions thrown when reading the resulting observable.-
options.exRead.catch: Boolean|Function
Defaulttrue
.true
Catch allTypeError
exceptionsfalse
Catch no exceptionsFunction
Syntaxcatch(error) : Boolean
Function
is called with the thrown exception. Returntrue
to catch the exception.
-
options.exRead.useDefault : Boolean
Defaultfalse
. Iftrue
, caught exceptions will result in the default value as defined byoptions.exRead.defaultValue
oroptions.exRead.defaultFunc
. -
options.exRead.defaultValue: value
Defaultundefined
. The value to return when an exception is caught. This value is overridden byoptions.exRead.defaultFunc
. -
options.exRead.defaultFunc: Undefined|Function
Defaultundefined
.Undefined
Not used.Function
SyntaxdefaultFunc() : value
. The result of this function is used as the return value when an exception is caught. This value overridesoptions.exRead.defaultValue
.
-
-
options.exWrite
Object literal containing options for handling exceptions thrown when writing to the resulting observable.-
options.exWrite.catch: Boolean|Function
Defaulttrue
.true
Catch allTypeError
exceptionsfalse
Catch no exceptionsFunction
Syntaxcatch(error) : Boolean
Function
is called with the thrown exception. Returntrue
to catch the exception.
-
options.exWrite.useDefault : Boolean
Defaultfalse
. Iftrue
, caught exceptions will write the provided default value to the underlying observables. -
options.exWrite.defaultValue: value
Defaultundefined
. The default value to use when an exception is caught. This value is overridden byoptions.exWrite.defaultFunc
. -
options.exWrite.defaultFunc: Undefined|Function
Defaultundefined
.Undefined
Not used.Function
SyntaxdefaultFunc() : value
. The result of this function is used as the default value when an exception is caught. This value overridesoptions.exWrite.defaultValue
.
-
-
options.pure
Defaulttrue
. This option is passed toko.computed
to select the usage of pureComputeds. -
options.deferEvaluation
Defaulttrue
. Iffalse
, the resulting observable is evaluated once (using .peek()) during extension.
Create a computed observable which only accepts a specified list of types. Does not perform conversions.
Create a computed observable which converts to and from internal and external types.