Scoping issue - calling an internal dojo function from external function

64 Views Asked by At

I have a function that is outside of the require portion of the dojo functions.

This function needs to call a function that resides within the dojo require block.

How do i call a function that is within the require code block from a function that resides outside the dojo require block?

Perhaps a little more application flow will demonstrate the need

  • Main window application spawns a child window
  • Main window sends a message to the child window that has a global function that will receive the message
  • Child window receives the message
  • The external function parses the message and determines that the map needs to be updated (The child window that is spawned is the mapping window and loads a lot of ESRI modules in the require section)
  • the child window function needs to call a function that is within the require code block of dojo to do the actual ESRI related tasks
2

There are 2 best solutions below

0
On BEST ANSWER

Talk about a hack... here is what i did to get the results i needed.

I created a hidden button on the form, bound the click event to fire off the function.

When the message was received and processed, I fired off the button click event - and viola!!

thanks everyone for the help.

2
On

It's a hacky solution and you should really think of a way to rearrange your modules if possible, but this should at least work:

var inside = null;

function outside () {
  try { inside(); }
  catch (err) { /* log error or throw away, whatever */ }
}

require(['dojo/_base/declare', ..., 'your/module/function'], function (declare, ..., myModuleFunction) {
  inside = myModuleFunction;
  outside();
});

Just require the module which contains the function (named "your/module/function" and myModuleFunction in the example), store it in a variable outside of the require and call it in a function which has been defined outside already. I added a try-catch block because it is good measure and prevents your code from blowing up if you call outside too early.

In case the function inside the dojo require block isn't a module, it's almost the same:

var inside = null;

function outside () {
  try { inside(); }
  catch (err) { /* log error or throw away, whatever */ }
}

require(['dojo/_base/declare'], function (declare) {
  inside = function () { console.log('Inside the require block'); };
  outside();
});

Except that you don't have to require it.