<modules.js> doesn't depend on those scripts.
However, you can get those scripts here
https://cixar.com/svns/javascript/trunk/src/
The trick is that modules.js contains some functionality like a
bare-bones XHR abstraction layer, environment sniffing, URL parsing,
and some browser abstractions that it requires to function. When I
wrote environment.js, browser.js and http.js, I realized that it would
be silly to duplicated this basic functionality in both the modules.js
and these basic modules. So, I decided to make modules.js "bootstrap"
module objects for those three, special modules. This way, if a
script loads <http.js>, modules.js will still go fetch the file and
evaluate it, but the module object has been pre-populated with URL
parsing, Request, Response, and createNativeRequest tools.
I hope that illuminates some of the dark corners. You're right that
it's a complex module loader; the notion was for it to consume a wide
variety of complexities so module authors and users wouldn't have to
think about them. I think you'll find it rather easy and safe to use
even though there's a lot to learn about how it works. modules.js is
like the JavaScript engine; you don't have to know how to parse and
evaluate JavaScript to write JavaScript programs, even though having a
good idea about how scope chains work really helps.
If you haven't tried it out yet, make a skeleton website with
modules.js. Make an index.html, index.js, and copy the modules.js
file into the same directory. Put some JavaScript in your index.js
and put <script src="modules.js?index.js"></script> in your
index.html. Check out whether it works and fiddle with your index.js
to satisfy yourself it's getting executed. You'll notice it's even
getting executed after the DOM loads but before images finish loading
in most browsers; yet another complexity you don't have to worry about
unless you want to read up on how modules.js does it (scary to me
even).
The next step would be to write a module and use it in index.js. Make
a dependency.js file in the same directory. Expose a function for you
to use in your index.js.
this.foo = function () {
return 'bar';
}
Then modify your index.js to include it and use it:
include('dependency.js');
alert(foo());
Give it a shot. It's super-easy to use, even though Pandora's demons
are locked up in it.
If you like making and using modules, and you need some bit of
functionality that you're sure someone's already written, drop the
list an email again. I can hook you up with lots of modules in the
Chiron library that have been ported to take advantage of the system.
You can get Steve Levithan's regular expression abstraction layer, or
encoders, decoders, and hashing algorithms, a really advanced object
oriented type system, css selectors, animations, memory-safe widgets,
advanced event observer pattern tools, and a whole lot of other stuff
I've collected.
Kris
So, knowing this, this is how you would have to write your example:
> test.js:
>
> this.t1 = function ()
> {
> alert("t1");
> }
>
> include("test2.js");
> t2();
>
> --------------------------
> test2.js:
>
> this.t2 = function ()
> {
> alert("t2");
> }
>
> include("test.js");
> t1();
>
> -----------------------
> index.html :
> <head>
> <script src="js/modules.js?test.js"></script>
> </head>
Note that you only need to include one script in the module in the
<script> tag. The include statements will tell modules.js when they
need other modules and load them as needed. Also, I added the
necessary "include()" calls to your modules. Normally I would but
them at the top of the file, but your modules have an unusual
interdependency relationship: they both require functions declared in
the other. To make this work, I declare the exportable items first so
they're available in their opposite module.
I hope this illustrates the value of modules.js. You don't have to
load all of your modules up front. Someone can provide you an entire
system of modules without you having to know any of the details about
its files and their dependencies: just include() the one you want.
Thanks again,
Kris
On Thu, Aug 14, 2008 at 2:36 AM, Johnny Yangthur <yes...@gmail.com> wrote:
> i just have try the tutorial on <http://modulesjs.com/samples.html >,
> and it is work. and some page seems didn't update to time:
> http://modulesjs.com/tutorial.html : chapter <Inside a Module > :
>
> ------------------------------------
>
> To create a function:
>
> this.f = function () {};
>
> To use it in this or any other module:
>
> f();
>
> ------------------------------------------------
>
> what i try is:
>
> test.js:
>
> this.t1 = function ()
> {
> alert("t1");
> }
> t2();
>
> --------------------------
> test2.js:
>
> this.t2 = function ()
> {
> alert("t2");
> }
> t1();
>
> -----------------------
> index.html :
> <head>
> <script src="js/modules.js?test.js&test2.js"></script>
> </head>
>
> -----------------
>
> doesn't work, so according to you last post to me , that becasue i
> lost a " include('test*.js') " statement in each Test*.js module, but
> the first time i reference the <http://modulesjs.com/tutorial.html > ,
> it is really confuse me.
http://modulesjs.com/nightly/build/
Or the nightly source directory:
http://modulesjs.com/nightly/src/
Or download a complete build from the last ".zip" file in the nightly build:
Or using Subversion (a source control tool that you can download,
http://tortoisesvn.tigris.org/) with the repository URL:
https://cixar.com/svns/javascript/trunk/src/
However, I don't want to lead you astray. You don't need any of these
files to use modules.js to manage your own code with it. You might
find some handy modules you would want though.
In response to your second question, I believe your understanding is
correct. If you download http.js, you can include it in your modules
and use the public interface. The functions written in modules.js are
for its own use. http.js wraps them up for public use. You might
actually want to do that because some of those functions are really
handy.
Kris