tags:

views:

69

answers:

1

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.

A: 

I believe, your dojo.hitch is processed at the time of the call to the post function. Meaning the hitch is inside the SearchWidget, and this will reference that. So, the call back function is actually in SearchWidget... (You are passing the function created by dojo.hitch in searchwidget - not a call to dojo.hitch.)

I have noticed DOJO doing stuff like this that was really screwy. I have gone as far as using global variables and being very explicit in the way I call the functions in order to daisy-chain multiple widgets. (Very messy)

Ruz
I hate creating global functions for something like this but had to go that route, as I have in the past. I wonder how the dojo "experts" work around these sorts of issues, I've been using dojo for over 3 years and still haven't figured a way around this.
Justin