In the script code, he created a binding handler for the bootstrap switch, which updates the knockout when the switch is executed: in the init function
init: function (element, valueAccessor, allBindingsAccessor, viewModel) { var $elem = $(element); $elem.bootstrapSwitch(); $elem.bootstrapSwitch('setState', ko.utils.unwrapObservable(valueAccessor()));
it receives a control in which the knockout property is bound and uses jquery to place a handler to update the knockout property when "change-change"
and if there is an update and the new value is not the current value, it updates the knockout
update: function (element, valueAccessor, allBindingsAccessor, viewModel) { var vStatus = $(element).bootstrapSwitch('state'); var vmStatus = ko.utils.unwrapObservable(valueAccessor()); if (vStatus != vmStatus) { $(element).bootstrapSwitch('setState', vmStatus); } }
for more information on custom bindings, please visit: http://knockoutjs.com/documentation/custom-bindings.html
In the question ":" in your code, you defined your model as a function, therefore we use the symbol "=" to determine its properties while in the script code it uses an object, therefore we use ":" to determine its properties
source share