javascript - Knockout JS - data-bind multiple values -


question js knockout library - have 3 inputs, data-bind-ed same variable. 2 have boolean value of false , 1 has boolean value of true. (i can't change them ints, unfortunately, make problem easier). although 2 false-valued inputs share behavior, need differentiate between them somehow trigger different behaviors.

is possible data-bind each variable, different values? instead of each being

 <input data-bind="checked:test" value="false"> 

i have like

 <input data-bind="test, test2" value="false, 1"> 

and

 <input data-bind="test, test2" value="false, 2">? 

i tried directly , didn't work don't know if it's possible. much.

you cant bind multiple variables directly creating custom bind function trick you.

example : http://jsfiddle.net/gurkavcu/epw8y/
** change input value (true , false) trigger update function

html

<input data-bind="customdata: test , v1 : test2"/> <div> <span data-bind ="text : test"/> </div> <div> <span data-bind ="text : test2"/> </div> 

js

ko.bindinghandlers.customdata = { init: function(element, valueaccessor, allbindingsaccessor, viewmodel) { $(element).change(function () { valueaccessor()(element.value); }); }, update: function(element, valueaccessor, allbindingsaccessor, viewmodel) { var value =ko.utils.unwrapobservable(valueaccessor()); var v1 = allbindingsaccessor().v1; if(value === "true") { v1("1"); console.log(v1()); } else if(value === "false") { v1("2"); console.log(v1()); } } }; function viewmodel() { this.test = ko.observable(false); this.test2 = ko.observable("2"); }; $(function() { var viewmodel = new viewmodel(); ko.applybindings(viewmodel); })รข€‹ 

modify update function needs. can add number of variable binding v1 : ... , v2 : ... , v3 : ... , access via allbindingsaccessor().v1 , allbindingsaccessor().v2 , allbindingsaccessor().v3


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -