Hey folks,
I have a class created using dojo.declare. Within the class I have a function/method which in turn calls dojo.xhrPost with the load: pointing to another function within the class. When the handler function is called, I can no longer call other functions within my class from that handler function as the 'this' object is changing. I would like to be able to call a class function from within a handler function. Here is an example:
dojo.declare(
"MyClass",
... [snip] ...
get_post_data: function () {
console.log(this) // This displays the this object, i.e. the current MyClass object
dojo.xhrPost({
url: 'my/url/here',
content: {'content': this.content},
load: this.handler_function,
error: this.error_handler,
handleAs: 'json'
});
},
handler_function : function(response, ioArgs) {
console.log(this); // 'this' is no longer the MyClass object.
// All I want is to be able to do the following:
this.another_function(); // should call the function below, but 'this' object has changed!
return response;
},
another_function : function() {
console.log('success!');
}
... [snip] ...Any ideas? I just need a way to have access to my class instance again, so I can call (in the example above) another_function from handler_function.
Thanks for any time you put into my question!

Found a solution
If there is a better or cleaner or more "proper" way to do this, anyone please pitch in, but here's how I solved my issue:
get_post_data: function () { dojo.xhrPost({ url: 'my/url/here', origin: this, // here is the solution!! content: {'content': this.content}, load: this.handler_function, error: this.error_handler, handleAs: 'json' }); }, handler_function : function(response, ioArgs) { // 'this' has still changed, but I can access my old 'this' by: this.origin.another_function(); // now another_function() will be called and success is printed to the console! return response; }, another_function : function() { console.log('success!'); } ... [snip] ...Hope this helps someone...
there's a better way.
Here you go:
...
I <3 dojo.hitch
Yeah, dojo.hitch is the way to go. Ever since I found that gem, I use it all over the place.
hitch it is!
That's a clever idea too, cool stuff.
Thanks!
Okay, for the sake of
Okay, for the sake of completeness:
http://dojocampus.org/content/2008/03/15/jammastergoat-dojohitch/
Enjoy.
We need a dojo.partial article though, which is just like hitch but assumes a global scope.
foo(0.4);
Jared wrote one...
...as a blog post here on dtk:
http://dojotoolkit.org/2008/04/09/dojo-data-notification-events-and-easy...
He wrote it in the context of dealing with dojo.data handlers but it's pretty complete, and entirely based on dojo.partial.
Cheers.