Saturday, February 23, 2013

Knockout Advanced Tips: Binding Custom Parameters

It is possible in Knockout to bind custom parameters to a call in JavaScript. I believe this is intrinsic to JavaScript. It also kind of reminds me of currying though at the moment I am not entirely sure.
For example, suppose you wanted to call myFunction with some parameters besides event and item (because those are what you get normally by Knockout). I need to do this because I want a callback after the click event. So here is my function:

var myFunction = function(callback, data, event) {

   //value of "this" would be $data

Here is how I would do the call:

click: myFunction.bind($data, myCallbackFunction);
The first argument to "bind" is the context (value of "this") that you want the function to execute with. Any arguments after that will be passed to the function prior to the actual arguments.

Data and event are always passed after any extra arguments that you provided to "bind".

1 comment:

robuxword said...

swagbucks coupons
Swagbucks is a mainstream rewards program that gives gift vouchers and money for online exercises. Discover is Swagbucks genuine in this fair Swagbucks audit.