PDA

View Full Version : [1.1 Beta 1]: EditorGrid event, anonymous function is called, named function isn't?


willydee
06-22-2007, 05:05 AM
Strange problem I discovered today (possibly my misunderstanding?):


/**
* Prerequisites: Singleton class, creating a LayoutDialog with embedded GridEditor
* and providing a reference to submit button
*/

// this does work:
this.grid.on('afteredit', function() { this.button.enable() }, this);

// this doesn't:
this.grid.on('afteredit', this.button.enable, this);

neongrau
06-22-2007, 08:20 AM
thats something that irritated me too.

try

this.grid.on('afteredit', this.button.enable, this.button);


i think this way it should work

willydee
06-22-2007, 12:51 PM
Hmm, that doesn't seem to be the logical way, since the handler function is scoped to button then, so "this" should be "button", no? ... Anyway, I'll try it.

EDIT:
You're right, it works, but I desperately need an explanation... 8-|

Animal
06-23-2007, 02:22 AM
The function that you are requesting to be called is "this.button.enable".

The object that must be the "this" reference for the duration of that function is "this.button"

willydee
06-23-2007, 03:20 AM
What's the scope when calling this.button.enable() from inside an anonymous function then?

Animal
06-23-2007, 07:33 AM
It can be anything. It depends how the function is called. See the javascript docs for the Function class.

You use


myFuncRef.apply(myFuncScope, [args]);


And that uses "myFuncScope" as the "this" reference.

in this:


this.grid.on('afteredit', function() { this.button.enable() }, this);


The "on" method is passed the current value of "this" to bind as the anonymous function's scope.

willydee
06-23-2007, 08:00 AM
Thanks, Animal.