I have 2 dijit widget template classes and am trying to call one from the other but am having issues with dojo.hitch and scope:
I'm in a searchvehicleswidget and call a second widget, commandswidget:
this.CommandsWidget.Post("vehicle/getconfiguration/", request, dojo.hitch(this.CommandsWidget, this.CommandsWidget.FillForms));
Which then calls the Post function in commandswidget:
Post: function(path, request, callbackFunction) {
console.debug("in post");
console.debug(this);
dojo.xhrPost({
url: baseUrl + path,
handleAs: 'json',
timeout: 60000,
content: request,
contentType: "application/x-www-form-urlencoded",
load: dojo.hitch(this, function(result) {
console.debug("in callback function");
console.debug(this);
callbackFunction();
}),
error: function(error, args) { AjaxError(error, args, path, request, callbackFunction); }
});
}
Which then calls the callback function originally passed in, FillForms:
FillForms: function(json) {
console.debug("in fillforms");
console.debug(this);
}
When it calls the Post function, the scope is CommandsWidget, which is correct. Even when we get into the return function for xhrPost load, the scope is still correct, CommandsWidget. However, when I then invoke "callbackFunction();", which calls FillForms(), the scope reverts back to SearchVehiclesWidget! Even if I wrap that call in a dojo.hitch(this, callbackFunction), it still has a scope of SearchVehiclesWidget.
Anyone have any insights into what could be going wrong and how to fix it?
No, CommandsWidget is an instance of the Commands class and SearchVehiclesWidget is an instance of the SearchVehicles class. Yes, FillForms and Post are both in Commands.