Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout JS - data-bind multiple values

Question about JS Knockout library - I have three inputs, all data-Bind-ed to the same variable. Two have a boolean value of false and one has a boolean value of true. (I can't change them to ints, unfortunately, which would make this problem easier). Although the two false-valued inputs share behavior, I need to differentiate between them somehow to trigger slightly different behaviors.

Is it possible to data-bind each to another variable, with different values? So instead of each being

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

I would have something like

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

and

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

I tried that directly and didn't work so I don't know if it's possible. Thanks so much.

like image 867
user1436111 Avatar asked Jul 07 '12 00:07

user1436111


People also ask

What is two way data binding in knockout JS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.

Can we have multiple Knockout model?

Knockout now supports multiple model binding. The ko. applyBindings() method takes an optional parameter - the element and its descendants to which the binding will be activated.

What are the types of binding supported by knockout JS?

Binding Values The binding value can be a single value, literal, a variable or can be a JavaScript expression.

How do I assign a value to Knockout observable?

To create an observable, assign the ko. observable function to the variable. A default value can be specified in the constructor of the call. Knockout then converts your variable into a function and tracks when the value changes, in order to notify the UI elements associated with the variable.


1 Answers

You cant bind multiple variables directly but creating a custom bind function do the trick for you.

Example : http://jsfiddle.net/gurkavcu/ePW8Y/
** Change input value (true , false) to trigger the 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 the update function for your needs. You can add any number of variable to the binding with v1 : ... , v2 : ... , v3 : ... and access it via allBindingsAccessor().v1 , allBindingsAccessor().v2 , allBindingsAccessor().v3

like image 75
Luffy Avatar answered Oct 18 '22 11:10

Luffy