I like to know what is the difference between Init and the knockout js custom binding update feature?
when should we write code inside the init function, and when should we go for the jockout js custom binding update function.
I am looking through a js doc knockout, but still my understanding for jockout js custom binding is not very clear to me.
I am just wondering if anyone helped to understand when we will go on init, and when we will start updating with an example.
here I point out some code that is not very clear and their intention.
Code for the Init function
<div data-bind="allowBindings: true">
<div data-bind="text: 'Replacement'">Original</div>
</div>
<div data-bind="allowBindings: false">
<div data-bind="text: 'Replacement'">Original</div>
</div>
ko.bindingHandlers.allowBindings = {
init: function(elem, valueAccessor) {
var shouldAllowBindings = ko.unwrap(valueAccessor());
return { controlsDescendantBindings: !shouldAllowBindings };
}
};
Code for update function
<input type="text" data-bind="value: someText, customBinding: someText">
ko.bindingHandlers.customBinding = {
update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var value = ko.utils.unwrapObservable(valueAccessor());
console.log( JSON.stringify(value) );
}
}
ko.applyBindings({
someText: ko.observable("inital value")
});
source
share