On Mon, Dec 10, 2012 at 7:55 PM, Jakob Heuser <
ja...@felocity.com> wrote:
> Is there any reason that we should support exports and init in the shim? In
> theory a init of "init: function() { return some.thing }" would obviate the
> need for the exports parameter and save on the conversion of a string into
> an object path. Alternatively, since a lot of define() operates on an
> overloaded API, we could have either init or exports serve both roles.
I actually had something more similar to this in requirejs 2.0 but
modified it for 2.1 -- by always having exports as a string, it allows
the loader to check for a load failure in old IE. The init function
some folks wanted because it allowed returning a slightly modified
export, maybe doing a noConflict, but by also specifying the "exports"
string, the loader could verify the script loaded correctly and if
not, properly trigger errbacks.
The string was important because the checking of the exports value
could happen twice: once in the "did things load correctly" stage of
the loader, and then later to actually set the exports value. The
init() function however can modify state (noConflict() being the
worst) so running the init function twice in place of using an exports
string would cause problems.
I expect most folks to just use the string exports and not specify
init(). However, there was a bug report for a 2.1.x requirejs release
when I mandated always having an exports string about allowing init
even if the exports string was not there. They did not care about the
old IE handling in that case, and as I recall wanted to compose the
exports out of a few different globals.
That is some background on the forces involved, but I'm open to
something else if it satisfies those forces.
> On the section about jQuery plugins, the correct export is "technically"
> window.jQuery, right? This way you can just depend on the plugin. I suppose
> that is more of a best practices question.
For a jquery plugin, the deps value should just be "jquery" -- those
deps values should be module IDs. For the exports check, I would use
"jQuery.fn.pluginName", and not use "window" on the front.
This is more of my preference to avoid using "window" explicitly when
I can for something that is reflected in the global space. The global
space could be some environment, not "window". The jQuery source code
explicitly attaches to window and not the "this" value it could use
for the immediately invoked function it uses around its code, but the
user could be using something else to fulfill the "jquery" dependency
(like zepto) and those libs may do something different and may even
work in an env where window is not there.
So, no solid slam dunk on the reasoning, just wanting to avoid extra
typing and discouraging the use "window" as a global.
Good suggestion. I did a quick pass at an example that includes
underscore, Backbone and a jquery plugin to the end of the section:
https://github.com/amdjs/amdjs-api/wiki/Common-Config#wiki-shim
feel free to modify if you think it should be fleshed out more.
James