I'm using Google Closure's event handling like this:
goog.events.listen(this.contentElement, goog.events.EventType.CLICK, this.openEditor);
But I need to pass a string as an argument into the function which would be this.openeditor
I've looked through the documentation but can't seem to work out how to do this, anyone got any ideas?
Thanks!
I'm using Google Closure's event handling like this:
goog.events.listen(this.contentElement, goog.events.EventType.CLICK, this.openEditor);
But I need to pass a string as an argument into the function which would be this.openeditor
I've looked through the documentation but can't seem to work out how to do this, anyone got any ideas?
Thanks!
Share Improve this question edited Nov 28, 2012 at 19:39 Chad Killingsworth 14.4k2 gold badges37 silver badges59 bronze badges asked Nov 27, 2012 at 11:53 A_PorcupineA_Porcupine 1,0382 gold badges13 silver badges23 bronze badges 2-
Are you trying to fire the event, or are you trying to make it so that when the event is fired,
this.openEditor
will be passed a certain string? – Joshua Dwire Commented Nov 27, 2012 at 13:00 - The latter. The function will be called whilst passing the String. – A_Porcupine Commented Nov 27, 2012 at 13:23
2 Answers
Reset to default 8Try using goog.partial, like this:
goog.events.listen(this.contentElement, goog.events.EventType.CLICK, goog.partial(this.openEditor,'the string you want to pass it'));
When this.contentElement
is clicked, this.openEditor
will be called. The fist parameter will be the string and the second parameter will be an event object.
Above answer from Joshua is right - just want to add some more info to it.
goog.bind vs goog.partial defined in base.js
In goog.partial the context is set to current context - it returns a function which gets executed in current context.
goog.partial = function(fn, var_args) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
// Clone the array (with slice()) and append additional arguments
// to the existing arguments.
var newArgs = args.slice();
newArgs.push.apply(newArgs, arguments);
return fn.apply(this, newArgs);
};
};
Where as in goog.bind ( which actually checks for native implementation of bind) and you can pass the context as second parameter
goog.bind = function(fn, selfObj, var_args) {
//defined in base.js
....
....
return function() {
return fn.apply(selfObj, arguments);
};
}