I want to know what is the best practice or how everyone organises there
javascript files. I initially use to just embed all my js in the html
file but that gets messy quick. Now I create an application.js file that
I reference on all pages. This is slightly better but often there is code
only specific to a page. Do you guys embed that in the html page?
You might want to take a look at requireJS:
http://requirejs.org/
https://github.com/jrburke/requirejs
Seems like it would be a great place to start if you want to avoid
reinventing the wheel.
If you are just making the transition from in-HTML javascript towards
javascript-controlled modular organisation, you might find this blog
post of mine useful:
http://libraryinstitute.wordpress.com/2010/12/01/loading-javascript-modules/
It is a step by step demo of the transition, with comments and code
(switching from the end state to one possible usage pattern of requireJS
should only be a minor change, so you can see it as an introduction to
requireJS, which has lots more features and testing).
Here is the table of contents:
Loading Javascript Modules
What is the Module Pattern, anyway?
Step 0 - separate Files are not enough
Step 1 - wrap scripts in module pattern
Step 2 - make exports explicit
Step 3 - make import dependencies explicit
Step 4 - move module loading to Javascript
Step 5 - sort imports in dependency order
Step 6 - reusable module loading and inline modules
References
Bonus steps
You still have to organise your code to suite your project,
but the steps above (or any module loader) give you the
control framework to handle that organisation. Proper
module loaders let you specify mappings of modules to
files, or deliver multiple modules from a single file.
I hope that the simple blog post might also serve as a base line
from which to try understanding the more complex aspects of
ongoing discussions surrounding modules and module loaders.
There tends to be lots of discussion about modules and module
loaders on the commonjs mailing list, and there is a strawman
proposal for simple modules for future versions of javascript.
Claus
PS. I've been following this group via the gmane gateway
http://dir.gmane.org/gmane.comp.lang.javascript.jsmentors
and have posted successfully before, but my last attempt
ended up with jsmentors-bounces@.. , with no apparent
progress. I noticed that the gmane configuration mentions
an @jsmentors.com address, rather than the newer
@googlegroups.com address - does that need to be
updated, or is a forwarding mechanism in place?
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]