#14690 closed enhancement (fixed)
AMD counterpart for dojo.global
Reported by: | vvoovv | Owned by: | Rawld Gill |
---|---|---|---|
Priority: | low | Milestone: | 1.8 |
Component: | Documentation | Version: | 1.7.1 |
Keywords: | Cc: | Bryan Forbes | |
Blocked By: | Blocking: |
Description
http://livedocs.dojotoolkit.org/dojo/global hasn't been updated to reflect AMD syntax.
Should .global be accessed from dojo/_base/kernel or from dojo/_base/window ?
Change History (7)
comment:1 Changed 9 years ago by
Owner: | set to Rawld Gill |
---|---|
Status: | new → assigned |
comment:2 Changed 9 years ago by
Milestone: | tbd → 1.8 |
---|---|
Priority: | undecided → low |
Type: | defect → enhancement |
On the browser, dojo.global is really only useful/interesting when used with the dojo/_base/window API because you can otherwise just write window
.
On some other environments, the only way to reliably get to the global object without using some special variable (e.g., node's process.global
) it so call a function without context like this:
var global = (function(){return this;})(); // global is now guaranteed to be the global object
dojo/_base/kernel initializes dojo.global as such, so dojo.global could be useful syntax sugar for programs that work in several different non-browser environments.
comment:3 Changed 9 years ago by
I guess @rcgill is saying that when running in NodeJS you should use dojo/kernel::global, but in a browser you should use dojo/_base/window::global.
BTW, ideally this would be documented in the reference pages for those modules, http://livedocs.dojotoolkit.org/dojo/_base/kernel, which doesn't exist yet :-(, and http://livedocs.dojotoolkit.org/dojo/_base/window. In addition to being cross referenced from http://livedocs.dojotoolkit.org/dojo/global.
comment:4 Changed 9 years ago by
looking at this again I noticed
- dojo/_base/kernel returns dojo
- dojo/_base/window returns dojo.global *at the time the factory is run*...which is useless with using the rest dojo/_base/window API
So, the answer is: use dojo/_base/kernel.global (or just dojo.global).
Leaving open to update docs.
comment:5 Changed 9 years ago by
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I updated the docs a few weeks ago, closing this.
comment:6 Changed 9 years ago by
Cc: | Bryan Forbes added |
---|
Rawld, this is from Bryan's email, can you confirm?
In the AMD world, |this| within a factory function should always be the global object (someone please correct me if I'm wrong):
define([], function(){ var global = this; });
That seems simpler than adding dojo/_base/kernel to your dependency list.
Now, kernel.global is special because it's value can be altered by dojo/_base/window::setContext() and winGlobal(), or by someone just setting kernel.global = foo... but for server-ish code that feature (or side effect) doesn't seem useful, do you agree? As an example see dojo/string.js which defines substitute() as:
string.substitute = function( /*String*/ template, /*Object|Array*/map, /*Function?*/ transform, /*Object?*/ thisObject){ ... thisObject = thisObject || kernel.global; ... }
comment:7 Changed 8 years ago by
Updated the docs again, see https://github.com/dojo/docs/commit/c5a428e48191b6ff214dff1e7f1e9c99a6efb6e4.
I get confused by this too. IIRC Rawld said that at least sometimes global should be accessed from kernel, for code that's supposed to work in a server environment where there is no window. But in that case, what's the meaning of dojo.global anyway? (I found that manual page to be lacking.)