How to extend knockout observables to read default value from binding ?

How to extend knockout observables to read default value from binding ?

Asked on December 13, 2018 in knockout js.
Add Comment


  • 1 Answer(s)

        Make a unique custom binding handler is help achieve this:

    ko.bindingHandlers.initializeValue = {
        init: function(element, valueAccessor) {
            valueAccessor()(element.getAttribute('value'));
        },
        update: function(element, valueAccessor) {
            var value = valueAccessor();
            element.setAttribute('value', ko.utils.unwrapObservable(value))
        }
    };
    <input type="hidden" value="@Model.SomeValue"
        data-bind="initializeValue:myObservableReference, value: myObservableReference"/>
    
    Answered on December 13, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.