--
You received this message because you are subscribed to the Google Groups "amd-implement" group.
To unsubscribe from this group and stop receiving emails from it, send an email to amd-implemen...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I guess that means that dojo has never worked with the curl loader.
John, any ideas on how to get a reference to the global scope (or whatever its called)? James thought that the (function(){ return this; })() approach wouldn't work when "use strict" was set. Judging from http://stackoverflow.com/questions/5447771/node-js-global-variables perhaps we should get it as typeof global === "undefined" ? window : global.
Some may argue that AMD modules shouldn't try to set/get global variables, but OTOH it seems OK for applications to use global variables (ex: <div onclick="myOnclickMethod();">), and AMD modules may need to access those globals. That's the case for dojo's parser anyway.
Actually, we've got lots of folks using dojo (1.6-1.9) and curl.js together successfully. Some rather large projects, too.
Whenever I need to capture the global in a module (which is never -- unless I need to grab `window` or `document` to do DOM-ish things), I use an IIFE, like this:(function (global) {define(function () {// use global here});}(typeof window !== 'undefined' ? window : null));
There's never a use case wherein DOM0 events bound to global methods is the best solution.
On Wednesday, April 2, 2014 10:01:27 PM UTC+9, unscriptable wrote:Actually, we've got lots of folks using dojo (1.6-1.9) and curl.js together successfully. Some rather large projects, too.Yes, but what I meant was that dojo.global is presumably set incorrectly, and that will break various things, like when dojo/parser tries to look up globals, ex: <div data-dojo-type=dijit/tree store=myGlobal>. I didn't mean that every single dojo feature was broken.
Why use an IIFE rather than just something like this?define([....], function(){var global = typeof window !== 'undefined' ? window : null...});
You'll probably argue that we should desupport/discourage that capability too, but that's sort of a separate discussion.
I just peeked and found that dojo.global is set in dojo/_base/kernel. This is not an AMD-wrapped CommonJS module, so it works fine: https://github.com/dojo/dojo/blob/master/_base/kernel.js#L21
For maximum compat with node modules, curl.js sets the context to module.exports when the module is AMD-wrapped CommonJS. Otherwise, it's undefined.
--
For that reason, if we were to specify what `this` should be in the
AMD spec, then I think it should just always be the exports object. If
John and another AMD implementer like Rawld or Jakob agree, then I can
work up a pull request change to the AMD doc.
2) I am open to exposing `module.global` if modules needed access to
whatever the global is for the environment. This seems analogous to
the loader.global property in ES6 loaders. But also:
3) I agree with John that modules should avoid accessing a general
global object, and best to environment detect for specific globals or
global properties that are needed, like looking for `document` (or
asking for a 'document' module that does the detection or sets up a
shim) if needing something like `document.querySelector` in the
module.
4) requirejs has set the exports value to "this" for quite a while,
even back to the 1.0 series[1], although the 1.0 series had caveats to
it, so may not always have been observable. The commit Bill mentioned
was for fixing a specific issue around access to `module.exports`
inside the factory function.
James
[1] https://github.com/jrburke/requirejs/blob/1.0.8/require.js#L1762
On Tue, Apr 1, 2014 at 1:27 PM, Bill Keese <wke...@gmail.com> wrote:
> 1. client code should get this from a closure, i.e. (function(){ return
> this; })().foo instead of just this.foo
> 2. RequireJS shouldn't mess with the value of this inside the callback; it
> should point to the global
> 3. there should be a module.global property that points to the global object