Opened 14 years ago

Closed 14 years ago

#5400 closed defect (wontfix)

Functions defined in local modules are undefined if used with XDomain load if not explicitly bound to variables

Reported by: ptwobrussell Owned by: James Burke
Priority: high Milestone:
Component: Core Version: 1.0
Keywords: Cc: [email protected]
Blocked By: Blocking:


What I just spent a bit of time figuring out is that if you have a local module and dojo.require it in conjunction with an xdomain load, any symbols in it that aren't explicitly bound to a variable are not defined. For example, if my module is simply:


bar = "bar";
function baz() { /* ... */ }

I can dojo.require in the foo module and verify that bar is defined...but baz is not. Again, this is only the case with an xdomain load. Using the module with a local installation of dojo doesn't present this problem.

However, if I explicitly bind the function like

baz = function baz() { /* ... */ }

I don't have the problem.

Seems to me that this extra "binding to a variable" step shouldn't be necessary, so I wonder if it's a bug -- or if it's something to do with browser security, how modules are processed in conjunction with an xdomain build, or what?

At any rate, I want to write this up in my book as either a "this is the way you do it and here's why" or as a "this is currently the way you do it -- but this is a bug so keep an eye on this ticket in trac to see when it's fixed." In the former case, could you elaborate on exactly why this would necessarily need to be the case.

Change History (2)

comment:1 Changed 14 years ago by ptwobrussell

By the way, phiggins is the man. He helped me to figure out that functions would be defined if bound to variables...though neither of us can explain why.

phiggins for person of the year? anyone?

comment:2 Changed 14 years ago by James Burke

Resolution: wontfix
Status: newclosed

You see this behavior in xd loading because the module code is wrapped in a function. This is required for the dependency mapping to work in the asynchronous loading that happens with xd loading. So when you do

function foo() {}

That function is bound to the private scope within that wrapping xd function.

However, doing

foo = function() {}

avoids the problem because you are creating a global variable that points to that function.

Note that you should have had the same sort of problem that you saw in xd loading with regular dojo loading running under IE, since IE does not use the window object for the scope when running eval().

So you can take this as a sort of "make sure you namespace your code properly" enforcement. If you really want to make a global function, be explicit by saying: = function() {}

But people should be encouraged to namespace their code appropriately.

BTW, if #4573 goes into to the 1.1 codebase, this will change all types of dojo loading to use function wrappers around loaded modules, so in all cases you will get the xd loading behavior you mention above. So if that code goes in, at least Dojo will be uniform in its behavior, but it will be different behavior vs. doing just a plain script tag in HTML.

Note: See TracTickets for help on using tickets.