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

前端 未结 2 1460
迷失自我
迷失自我 2020-12-10 11:46

I have finally found the time to start learning KnockoutJS while building a new MVC4 application. I am trying to figure out the best way to initialize an observable value fr

2条回答
  •  半阙折子戏
    2020-12-10 12:16

    You could try creating your own custom binding handler to 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))
        }
    };
    
    
    

提交回复
热议问题