Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockoutjs computed passing parameters

I am wondering if it is possible with knockoutjs to pass arguments when binding.

I am binding a list of checkboxes and would like to bind to a single computed observable in my viewmodel. In my viewmodel (based on parameter passed to the read function) I want to return true/false based on certain conditions.

var myViewModel=function(){     this.myprop=ko.computed({read: function(){     //would like to receive an argument here to do my logic and return based on argument. } }); };  <input type="checkbox" data-bind="checked: myprop(someval1)" /> <input type="checkbox" data-bind="checked: myprop(someval2)" /> <input type="checkbox" data-bind="checked: myprop(someval3)" /> 

Any suggestions?

like image 460
Qaiser Iftikhar Avatar asked Nov 03 '12 14:11

Qaiser Iftikhar


People also ask

What is Ko computed in Knockout JS?

In some scenarios, it is useful to programmatically determine if you are dealing with a computed observable. Knockout provides a utility function, ko. isComputed to help with this situation. For example, you might want to exclude computed observables from data that you are sending back to the server.

Which function is used to perform Knockout computation?

Computed Observable is a function which is dependent on one or more Observables and automatically updates whenever its underlying Observables (dependencies) change.

What is Ko pureComputed?

Syntax. The standard method of defining a pure computed observable is to use ko.pureComputed : this .fullName = ko.pureComputed( function () { return this .firstName() + " " + this .lastName();

How do you activate a Knockoutjs model?

To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel); You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery's $ function.


1 Answers

Create a function whose sole purpose is to return a computed observable. It may take in parameters as you wanted. It will have to be a separate computed observable if you want it to be a two-way binding.

Then in your binding, call that function with the appropriate arguments. The computed observable it returns will be bound to in your view and will update as usual.

Here's a fiddle where I used this technique for creating event handlers. You can do something similar here.

You can keep it clean by making the function a method on the observable. Either by adding to the ko.observable.fn prototype or adding it directly to the observable instance.

ko.observable.fn.bit = function (bit) {     return ko.computed({         read: function () {             return !!(this() & bit);         },         write: function (checked) {             if (checked)                 this(this() | bit);             else                 this(this() & ~bit);         }     }, this); }; // or function ViewModel() {     this.flags = ko.observable(0);      this.flags.bit = function (bit) {         return ko.computed({             read: function () {                 return !!(this() & bit);             },             write: function (checked) {                 if (checked)                     this(this() | bit);                 else                     this(this() & ~bit);             }         }, this);     }.bind(this.flags); }     

Then apply to your view

<input type="checkbox" data-bind="checked: flags.bit(0x1)"/> <input type="checkbox" data-bind="checked: flags.bit(0x2)"/> <input type="checkbox" data-bind="checked: flags.bit(0x4)"/> <input type="checkbox" data-bind="checked: flags.bit(0x8)"/> 

Demo


However if you are just trying to bind all those checkboxes to a single value in your view model, you don't need to do that. Use the checked binding on an array in your view model and give your checkboxes a value. Every checked value will be added to the array. And it will be a two way binding.

<input type="checkbox" data-bind="checked: checkedValues, value: 1"/> <input type="checkbox" data-bind="checked: checkedValues, value: 2"/> <input type="checkbox" data-bind="checked: checkedValues, value: 3"/> <input type="checkbox" data-bind="checked: checkedValues, value: 4"/> 
var viewModel = {     checkedValues: ko.observableArray([]) }; 

Demo

like image 106
Jeff Mercado Avatar answered Sep 28 '22 11:09

Jeff Mercado