Joachim Lundgren writes:
...
> Imagine a file A.js that requires B.js and checks if the file already
> has been loaded else loads it (with writing a SCRIPT tag) the
> required file (B.js) is loaded _after_ the current JavaScript-block
> or file has been executed - not inserted instead of the SCRIPT tag
> that was written.
>
> If you can come up with a solution I'll be more than interested!
If A.js requires B.js, it should be possible to queue up execution of
the code in A.js by wrapping all the code, including all function
definitions, in a function definition, say A_code. This may require
some changes to how variables are assigned. I.e. top level var
declarations. After you write out the SCRIPT tag to cause B.js to be
loaded, write out another SCRIPT block to call A_code().
But there is still a problem. If B.js in turn requires C.js, and we
similarly wrap the B code with a B_code function definition to defer its
execution, then the order of execution will be wrong:
load A.js (A_code is defined)
A loads B.js (B_code is defined, not executed)
A then calls A_code() - oops, B code has not been executed yet.
B loads C.js (C_code is defined)
B_code() - oops, C code has not been executed yet.
C_code()
We need to reverse the order of execution, so C_code executes first,
followed by B_code, and finally A_code. To reverse the order of
execution, instead of a queue, we need a stack. If we have a global
variable called Code_stack, each file can push its own execution on the
stack when it is loaded, and write SCRIPT tags to load everything it
requires. Then at the top level after everything has been loaded, we
just evaluate everything on the Code_stack from top to bottom.
So the order of execution becomes:
load A.js (push A_code)
load B.js (push B_code)
load C.js (push C_code)
C_code()
B_code()
A_code()
This algorithm should probably be modified to not execute a code block
if it has previously been executed. This could be implemented by having
each file set a file-specific global variable the first time it is
executed, and simply return if it has previously been set. e.g.
if (A_executed) {
return;
}
A_executed = true;
--
Daniel LaLiberte
[EMAIL PROTECTED]
http://www.HoloNexus.org/~liberte/
http://www.HyperNews.org/~liberte/
_______________________________________________
Dynapi-Dev mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dynapi-dev