Components of a binding handler
Binding handlers are defined by adding objects to the ko.bindingHandlers
object, just like extenders. They are composed of an init
and an update
function.
The init
function runs when the binding is first applied to the element either when ko.applyBindings
is called or when the element is created by a control flow binding, such as template
or foreach
. It should be used for all one-time work such as attaching event handlers or disposal callbacks to the element.
The update
function runs just after init
does, when the binding is first applied. It runs again anytime when any observable dependencies are changed. The update
function determines its dependencies just like a computed observable does. If an observable is accessed when an update runs, it subscribes to that observable. The update
function should be used to keep the UI in sync with changes from the viewmodel:
ko.bindingHandlers.yourBindingName = { init: function(element, valueAccessor, allBindings, viewModel...