knockout.js with checkbox checked bind when checked ,will trigger the update method in custom binging why -
var viewmodel={ ischecked:ko.observable(false); showmessage:ko.observable(); }; ko.bindinghandlers.dosomeing=function(){ update:function(element, valueaccessor, allbindingsaccessor, viewmodel, bindingcontext){ if(viewmodel.ischecked){ } } }; <pre> <input type="checkbox" data-bind="checked:ischecked"></input> <input type="text" data-bind="dosomeing:showmessage"></input> </pre>
when clicked checkbox trigger update method in custom binging dosomeing,why?
but remove if(viewmodel.ischecked) scope , not trigger update method .
the update
method in ko binding executed within computed observable. so, observables have value accessed (like viewmodel.ischecked()
in case, doing viewmodel.ischecked
not access value, might have typo in question), become dependency , cause binding's update function run again.
just note: bindings on element run within single computed, bindings on element run update functions again whenever dependency changes. these dependencies tracked each time, can change on time. in knockout 3.0, bindings run independently of each other on single element.
Comments
Post a Comment