knockout js pass parameter in knockout data binding using knockout safe binding


Arden Latraca

I've tried implementing ksb on my knockout js, but it's causing an error when passing parameters in the onClick binding.

I used this line:

<button type="button" data-bind="click: btnClick.bind($data, '1')">button</button>

It will throw an error:

Uncaught #<Object> knockout-secure-binding.js: 440

How to pass parameters on onClick binding?

Roy

The documentation for ksb says (note #3 in particular , in bold):

The language used in KSB in the bindings is a superset of JSON, but a subset of Javascript. For convenience, I'll call it slang.

The binding language is closer to JSON than the Java language, so it is easier to describe its differences by comparing it to JSON. The binding language differs from JSON in the following ways:

  1. it understands undefinedkeywords;
  2. It looks for variables on $dataor $contextor globals (in that order);
  3. Functions can be called ( but take no arguments );
  4. The top-level function is called to thisset an object with the following keys: $data, $context, a global variable, $elementcorresponding to the state used to bind the corresponding element.

To work around this limitation, you can use custom binding handlers that wrap other custom binding handlers and provide binding functions for them.

ko.bindingHandlers.curry = {
    unwrap: function (valueAccessor, data) {
        var val = valueAccessor(),
            theFunction = data[val[1].token];
        return {
            bh: ko.bindingHandlers[val[0]],
            fnVa: function () {
                return theFunction.apply.bind(theFunction, theFunction, val.slice(2));
            }
        };
    },
    init: function (element, valueAccessor, allBindingsAccessor, data, context) {
        var spec = ko.bindingHandlers.curry.unwrap(valueAccessor, data),
            init = spec.bh.init;
        if (init) {
            init(element, spec.fnVa, allBindingsAccessor, data, context);
        }
    },
    update: function (element, valueAccessor, allBindingsAccessor, data, context) {
        var spec = ko.bindingHandlers.curry.unwrap(valueAccessor, data),
            update = spec.bh.update;
        if (update) {
            update(element, spec.fnVa, allBindingsAccessor, data, context);
        }
    }
};

Use it like this:

<button type="button" data-bind="curry:['click', btnClick, '1', '2']">short</button>

Demonstration violin .

Related


Knockout.js data binding using jQuery

username I have a button that I want to bind to a method in the VM using knockout. I am using the following code <button type="button" class="btn btn-primary" id="cmdCreateConnection" data-bind="click: function(data, event) {

Knockout.js data binding using jQuery

username I have a button that I want to bind to a method in the VM using knockout. I am using the following code <button type="button" class="btn btn-primary" id="cmdCreateConnection" data-bind="click: function(data, event) {

Ajax data binding with Knockout Js

Kitty Sarvaj I am using knockout js and I am finding it difficult to bind data in ajax get method, I have created model, viewModel and ajax function, I have ajax method in the same js file where I am creating viewModel on page load ajax and try to bind my html

Ajax data binding with Knockout Js

Kitty Sarvaj I am using knockout js and I am finding it difficult to bind data in ajax get method, I have created model, viewModel and ajax function, I have ajax method in the same js file where I am creating viewModel on page load ajax and try to bind my html

Knockout data binding in function

BK52 Hi, I've tried a few things but I can't do that. HTML <div data-bind="dxTextBox: { value:my_var }"></div> JS var viewModel = { my_var: ko.observable(''), StartScan:function() { mytimer = setInterval(this.DataBind, 1000); }, DataB

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Knockout Select Data Binding

Chris I'm new to knockout and have a question about using data binding and "options" binding. I would like to have two dropdown lists with data from the database. I can get the first one to work, but even with the same implementation, the second one doesn't ap

Cleaner data binding in knockout?

Joe Scotto I'm new to using Knockout and am doing a very basic implementation to change colors in an observable way. Is there a cleaner way to write the following code? <div class="selected" data-bind="style: { background: fullHexCode(mainScreenNavigationSelec

Knockout js CSS binding

kitten salvaji I am using Knockout js in my project and I need to add multiple css class names to specific tags. Below is my code, I have three different classes, how can I add it to the knockout css binding, kindly suggest. <img data-bind="attr: { src:Profile

knockout js check binding

username I'm new to knockout and also a little confused about checkbox bindings. I have the following HTML: <input id="Category-2039842085241261997" type="checkbox" value="203984,208524,1261997" data-bind="checked:toggleActivation"> <label for="Category-2039

Knockout js binding not updating

User 135498 I have a view model that contains a reference to another object, and the referenced object is bound to my UI. When I add objects to the observable array, the bindings seem to work fine, but when I clear the array, the bindings don't seem to update.

Knockout js binding not updating

User 135498 I have a view model that contains a reference to another object, and that referenced object is bound to my UI. When I add objects to the observable array, the bindings seem to work fine, but when I clear the array, the bindings don't seem to update

knockout js check binding

username I'm new to knockout and also a little confused about checkbox bindings. I have the following HTML: <input id="Category-2039842085241261997" type="checkbox" value="203984,208524,1261997" data-bind="checked:toggleActivation"> <label for="Category-2039

Knockout js binding not updating

User 135498 I have a view model that contains a reference to another object, and that referenced object is bound to my UI. When I add objects to the observable array, the bindings seem to work fine, but when I clear the array, the bindings don't seem to update

Binding knockout updates in .js

Mike W This is most likely a very basic question for anyone familiar with ockout.js, but it's causing me problems. The situation I'm having is that I have a model that contains an array of items that are dynamically added to and displayed in a view. So far, no

Knockout using $index and if binding

User1255162: I'm trying to display some marker based on a value that I $indexcan display, but can't seem to get it to work with binding if, what's the best way to do this here? <!-- ko if: $index===0 --> <div>some mark up here</div> <!-- /ko --> John Earles

Knockout using $index and if binding

User1255162: I'm trying to display some marker based on a value that I $indexcan display, but can't seem to get it to work with binding if, what's the best way to do this here? <!-- ko if: $index===0 --> <div>some mark up here</div> <!-- /ko --> John Earles

Knockout using $index and if binding

User1255162: I'm trying to display some marker based on a value that I $indexcan display, but can't seem to get it to work with binding if, what's the best way to do this here? <!-- ko if: $index===0 --> <div>some mark up here</div> <!-- /ko --> John Earles

Knockout js: data binding click not working

Benjamin Baggins I can't trigger the knockout feature logMyStuffon my website with a simple button click . Note that I've added <button>, <a>and <div>tried to get it to work. I made a JS fiddle, but the JS fiddle worked. Here is my code: var SearchFilterViewMo

Knockout.js data binding to ViewModel

Damon I'm trying to figure out KnockOut data binding and am struggling to get a simple form to bind to a ViewModel. I am using WebAPI to extract my JSON data. This is my ViewModel, when this "find" method is called, it creates a new WorkOrder object and popula

Knockout.js data binding to ViewModel

Damon I'm trying to figure out KnockOut data binding and am struggling to get a simple form to bind to a ViewModel. I am using WebAPI to extract my JSON data. This is my ViewModel, when this "find" method is called, it creates a new WorkOrder object and popula