views:

670

answers:

2

Hi,

Using prototype.js, I create a custom object which is going to be a parameter on a constructor :

var options = Object.extend({month: date[0],
                       year: date[1],
                       day: date[2],
                          oncalchange: update});
// update is defined like that :
var update = function(d){
    // bla bla
}

// Calling my class
var myObject = new myClass (options);

On the oncalchange option, I have a callback function called "update", which takes a parameter within myClass.

Now, I want to pass an extra parameter to the "update" function, but not directly in the class.

I would like to do something like that :

// element is the parameter I want to add
var update = function(d, element){
    alert(element);
}

Obviously it doesn't work, how could I do something similar ?

+1  A: 

I'm not quite sure what you are trying to accomplish, but something like this?

var options = Object.extend({month: date[0],
                                 year: date[1],
                                 day: date[2],
                             oncalchange: function(d){ return update(d, 'somethingelse');}
                             });
Pim Jager
"element" is undefined using this.Actually I instantiate a calendar and oncalchange is an event function (Prototype.emptyFunction) called when I change the value in the calendar.
kevin
+1  A: 

in prototype, you can use the bind function (belonging to all functions) to pre-fill in values, and assign context. You can use the curry function instead if you do not need to use the context parameter

for example:

var options = Object.extend({month: date[0],
                                 year: date[1],
                                 day: date[2],
                             oncalchange: update.bind(this,arg1, arg2)});

or

var options = Object.extend({month: date[0],
                                 year: date[1],
                                 day: date[2],
                             oncalchange: update.curry(arg1,arg2)});

See http://www.prototypejs.org/api/function/bind for more info on bind, and http://www.prototypejs.org/api/function/curry for info on curryad

Edit

change your update function to:

var update = function(element,d){
    alert(element);
}

then pre-define the element using curry or bind as above.

I assume the callback is expecting a function with one parameter and you want it to take a second which you can define when setting the callback. In such case, the order of the parameters is very important. curry and bind can be used to pre-load arguments from the beginning, and as such, the parameters you want to pre-load should be defined before any of the other parameters.. Hopefully that clears things up.

Jonathan Fingland
Yeah, I tried the bind functions.But I get an "undefined" variable using bind or curry. update.bind(this, 'ok') ... update.curry('ok'); ... var update = function(d, el){ console.log(el); // el is undefined }
kevin
in your examples, you're only passing one value to the function. curry and bind pre-load from the beginning... I'll edit
Jonathan Fingland
Thanks but even if I change the order, "element" becomes "d" and "d" is undefined.
kevin
My bad, I was applying curry on the wrong function.Curry works great thanks.
kevin