What is the difference between the Init and update function from the knockout js user binding

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">
    <!-- This will display Replacement, because bindings are applied -->
    <div data-bind="text: 'Replacement'">Original</div>
</div>

<div data-bind="allowBindings: false">
    <!-- This will display Original, because bindings are not applied -->
    <div data-bind="text: 'Replacement'">Original</div>
</div>

ko.bindingHandlers.allowBindings = {
    init: function(elem, valueAccessor) {
        // Let bindings proceed as normal *only if* my value is false
        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")
});
+4
source share
1 answer

, :

init DOM . init :

  • DOM
  • , , , DOM,

update , (/), . , .

init , , update , . , , .

+1

All Articles