Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#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 8 years ago by bill

Owner: set to Rawld Gill
Status: newassigned

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.)

comment:2 Changed 8 years ago by Rawld Gill

Milestone: tbd1.8
Priority: undecidedlow
Type: defectenhancement

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 8 years ago by bill

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 8 years ago by Rawld Gill

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 7 years ago by bill

Resolution: fixed
Status: assignedclosed

I updated the docs a few weeks ago, closing this.

comment:6 Changed 7 years ago by bill

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;
...
}
Note: See TracTickets for help on using tickets.