> > Or is component the consensus, and I just have to deal with this?
> component is way too young to be a consensus
Ditto. Module loaders and patterns for the client-side are a bit like the
wild west right now -- there are a lot of competing module patterns and
module loaders. It's not like node, where almost everyone uses npm.
My recommendation for your situation is to follow the pattern that both
Underscore.js and Backbone.js use in order to work on the server and the
client without being opinionated about which client-side module loader is
in use, if any. Yes, it requires a bit of boilerplate in each JS file, but
really it's not much and it keeps things very simple. With it, your modules
will work in the node/npm/require() ecosystem and they will also work on
any browser without requiring people who want to use it on the client-side
into any particular module loader or module pattern or pre-processor. I've
been following this pattern for quite a while now and it's been working
great for me.
You can find the pattern by looking at the source (with more details
available on the annotated source) of those projects. In short, it involves
wrapping your script in an Immediately Invoked Function Expression like
this:
(function() {
var root = this;
// body of the module here
}).call(this);
The "root" variable now points to window on the browser and module.exports
on the server (the Backbone comments / annotations have been corrected on
this point, but the Underscore comments / annotations still incorrectly
state that it's `global` on the server. I wrote a blog post on the subject (
http://csnw.github.io/2013/06/23/this-in-node-modules-and-iifes.html), but
still need to issue a pull request.
To export the main namespace object, you can follow the code in Backbone
(the Underscore code that does the same thing is slightly more complicated
because it is still backwards compatible with an old require() API, which
IMO isn't necessary):
var Backbone;
if (typeof exports !== 'undefined') {
Backbone = exports;
} else {
Backbone = root.Backbone = {};
}
You can see the blog post mentioned above for variations on this theme.
Importing another module is also straightforward. I typically just check if
it's on the root, for instance, "var _ = root._ || require('underscore');"
Backbone is a bit more explicit and uses two lines to import underscore:
var _ = root._;
if (!_ && (typeof require !== 'undefined')) _ = require('underscore');
Best of luck! Let me know if you have questions.
-- peter rust
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.