View Full Version : [1.1 Beta 1]: EditorGrid event, anonymous function is called, named function isn't?
willydee
22 Jun 2007, 2: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
22 Jun 2007, 5: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
22 Jun 2007, 9:51 AM
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
22 Jun 2007, 11:22 PM
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
23 Jun 2007, 12:20 AM
What's the scope when calling this.button.enable() from inside an anonymous function then?
Animal
23 Jun 2007, 4: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
23 Jun 2007, 5:00 AM
Thanks, Animal.
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.