Skip to content
This repository

Node support: added Ember.ROOT == window or global or this. #769

Closed
wants to merge 2 commits into from

5 participants

Lance Pollard Don't Add Me To Your Organization a.k.a The Travis Bot Peter Wagenet envone Devin Torres
Lance Pollard

Added Ember.ROOT, equal to window if defined, global if defined, otherwise this. Added var ROOT = Ember.ROOT in relevant modules to optimize minification.

This makes it work in nodes. All tests still pass (running rake test[all] and rake test).

added some commits May 01, 2012
Lance Pollard added Ember.ROOT, equal to `window` if defined, `global` if defined, …
…otherwise `this`. added var ROOT = Ember.ROOT in relevant modules to optimize minification.
e7d15b8
Lance Pollard added Ember.ROOT, equal to `window` if defined, `global` if defined, …
…otherwise `this`. added var ROOT = Ember.ROOT in relevant modules to optimize minification.
9cbbddd
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged e7d15b8 into f968183).

Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 9cbbddd into f968183).

Devin Torres

Shouldn't this be var ROOT?

It's declared just above. e7d15b8#L3R8

Peter Wagenet
Owner
wagenet commented May 01, 2012

I like this idea, but I think we may want to discuss the implementation a bit more.

Lance Pollard

Any updates?

Peter Wagenet
Owner

This is still on our radar for 1.0 but we've been focusing on some other issues first.

envone
envone commented July 31, 2012

+1 on this feature.

Peter Wagenet
Owner

Since it's unlikely that we'll merge in this form I'm closing this PR in favor of #1434. @viatropos, we'll definitely take your work into consideration when we do finally get around to this feature.

Peter Wagenet wagenet closed this October 08, 2012
Peter Wagenet
Owner

I think this is mainly solved by 305202f. If there's more to do, we should make new tickets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

May 01, 2012
Lance Pollard added Ember.ROOT, equal to `window` if defined, `global` if defined, …
…otherwise `this`. added var ROOT = Ember.ROOT in relevant modules to optimize minification.
e7d15b8
Lance Pollard added Ember.ROOT, equal to `window` if defined, `global` if defined, …
…otherwise `this`. added var ROOT = Ember.ROOT in relevant modules to optimize minification.
9cbbddd
This page is out of date. Refresh to see the latest.
12  packages/ember-debug/lib/main.js
... ...
@@ -1,5 +1,5 @@
1 1
 /*global __fail__*/
2  
-
  2
+var ROOT = Ember.ROOT;
3 3
 /**
4 4
   Define an assertion that will throw an exception if the condition is not
5 5
   met.  Ember build tools will remove any calls to ember_assert() when
@@ -31,7 +31,7 @@
31 31
     will be executed.  If the function returns false an exception will be
32 32
     thrown.
33 33
 */
34  
-window.ember_assert = function ember_assert(desc, test) {
  34
+ROOT.ember_assert = function ember_assert(desc, test) {
35 35
   if ('function' === typeof test) test = test()!==false;
36 36
   if (!test) throw new Error("assertion failed: "+desc);
37 37
 };
@@ -50,7 +50,7 @@ window.ember_assert = function ember_assert(desc, test) {
50 50
     An optional boolean or function. If the test returns false, the warning
51 51
     will be displayed.
52 52
 */
53  
-window.ember_warn = function(message, test) {
  53
+ROOT.ember_warn = function(message, test) {
54 54
   if (arguments.length === 1) { test = false; }
55 55
   if ('function' === typeof test) test = test()!==false;
56 56
   if (!test) console.warn("WARNING: "+message);
@@ -70,7 +70,7 @@ window.ember_warn = function(message, test) {
70 70
     An optional boolean or function. If the test returns false, the deprecation
71 71
     will be displayed.
72 72
 */
73  
-window.ember_deprecate = function(message, test) {
  73
+ROOT.ember_deprecate = function(message, test) {
74 74
   if (Ember && Ember.TESTING_DEPRECATION) { return; }
75 75
 
76 76
   if (arguments.length === 1) { test = false; }
@@ -119,9 +119,9 @@ window.ember_deprecate = function(message, test) {
119 119
   @param {Function} func
120 120
     The function to be deprecated.
121 121
 */
122  
-window.ember_deprecateFunc = function(message, func) {
  122
+ROOT.ember_deprecateFunc = function(message, func) {
123 123
   return function() {
124  
-    window.ember_deprecate(message);
  124
+    ROOT.ember_deprecate(message);
125 125
     return func.apply(this, arguments);
126 126
   };
127 127
 };
17  packages/ember-metal/lib/accessors.js
@@ -13,6 +13,7 @@ var USE_ACCESSORS = Ember.platform.hasPropertyAccessors && Ember.ENV.USE_ACCESSO
13 13
 Ember.USE_ACCESSORS = !!USE_ACCESSORS;
14 14
 
15 15
 var meta = Ember.meta;
  16
+var ROOT = Ember.ROOT;
16 17
 
17 18
 // ..........................................................
18 19
 // GET AND SET
@@ -200,7 +201,7 @@ function normalizeTuple(target, path) {
200 201
       isGlobal = !hasThis && IS_GLOBAL_PATH.test(path),
201 202
       key;
202 203
 
203  
-  if (!target || isGlobal) target = window;
  204
+  if (!target || isGlobal) target = ROOT;
204 205
   if (hasThis) path = path.slice(5);
205 206
 
206 207
   var idx = path.indexOf('*');
@@ -215,7 +216,7 @@ function normalizeTuple(target, path) {
215 216
     }
216 217
     path   = path.slice(idx+1);
217 218
 
218  
-  } else if (target === window) {
  219
+  } else if (target === ROOT) {
219 220
     key = firstKey(path);
220 221
     target = get(target, key);
221 222
     path   = path.slice(key.length+1);
@@ -288,14 +289,14 @@ Ember.getPath = function(root, path, _checkGlobal) {
288 289
   // If there is no root and path is a key name, return that
289 290
   // property from the global object.
290 291
   // E.g. getPath('Ember') -> Ember
291  
-  if (root === null && !hasStar && path.indexOf('.') < 0) { return get(window, path); }
  292
+  if (root === null && !hasStar && path.indexOf('.') < 0) { return get(ROOT, path); }
292 293
 
293 294
   // detect complicated paths and normalize them
294 295
   path = normalizePath(path);
295 296
   hasThis  = HAS_THIS.test(path);
296 297
 
297 298
   if (!root || hasThis || hasStar) {
298  
-    ember_deprecate("Fetching globals with Ember.getPath is deprecated (root: "+root+", path: "+path+")", !root || root === window || !IS_GLOBAL.test(path));
  299
+    ember_deprecate("Fetching globals with Ember.getPath is deprecated (root: "+root+", path: "+path+")", !root || root === ROOT || !IS_GLOBAL.test(path));
299 300
 
300 301
     var tuple = normalizeTuple(root, path);
301 302
     root = tuple[0];
@@ -305,9 +306,9 @@ Ember.getPath = function(root, path, _checkGlobal) {
305 306
 
306 307
   ret = getPath(root, path);
307 308
 
308  
-  if (ret === undefined && !pathOnly && !hasThis && root !== window && IS_GLOBAL.test(path) && _checkGlobal !== false) {
  309
+  if (ret === undefined && !pathOnly && !hasThis && root !== ROOT && IS_GLOBAL.test(path) && _checkGlobal !== false) {
309 310
     ember_deprecate("Fetching globals with Ember.getPath is deprecated (root: "+root+", path: "+path+")");
310  
-    return Ember.getPath(window, path);
  311
+    return Ember.getPath(ROOT, path);
311 312
   } else {
312 313
     return ret;
313 314
   }
@@ -324,7 +325,7 @@ Ember.setPath = function(root, path, value, tolerant) {
324 325
 
325 326
   path = normalizePath(path);
326 327
   if (path.indexOf('*')>0) {
327  
-    ember_deprecate("Setting globals with Ember.setPath is deprecated (path: "+path+")", !root || root === window || !IS_GLOBAL.test(path));
  328
+    ember_deprecate("Setting globals with Ember.setPath is deprecated (path: "+path+")", !root || root === ROOT || !IS_GLOBAL.test(path));
328 329
 
329 330
     var tuple = normalizeTuple(root, path);
330 331
     root = tuple[0];
@@ -340,7 +341,7 @@ Ember.setPath = function(root, path, value, tolerant) {
340 341
       root = Ember.getPath(root, path, false);
341 342
       if (!root && IS_GLOBAL.test(path)) {
342 343
         ember_deprecate("Setting globals with Ember.setPath is deprecated (path: "+path+")");
343  
-        root = Ember.getPath(window, path);
  344
+        root = Ember.getPath(ROOT, path);
344 345
       }
345 346
     }
346 347
 
10  packages/ember-metal/lib/binding.js
@@ -11,6 +11,8 @@ require('ember-metal/utils'); // guidFor, isArray, meta
11 11
 require('ember-metal/observer'); // addObserver, removeObserver
12 12
 require('ember-metal/run_loop'); // Ember.run.schedule
13 13
 
  14
+var ROOT = Ember.ROOT;
  15
+
14 16
 // ..........................................................
15 17
 // CONSTANTS
16 18
 //
@@ -144,7 +146,7 @@ function empty(val) {
144 146
 
145 147
 /** @private */
146 148
 function getPathWithGlobals(obj, path) {
147  
-  return getPath(isGlobalPath(path) ? window : obj, path);
  149
+  return getPath(isGlobalPath(path) ? ROOT : obj, path);
148 150
 }
149 151
 
150 152
 /** @private */
@@ -564,10 +566,10 @@ Binding.prototype = /** @scope Ember.Binding.prototype */ {
564 566
         Ember.Logger.log(' ', this.toString(), '->', fromValue, obj);
565 567
       }
566 568
       if (this._oneWay) {
567  
-        Ember.trySetPath(Ember.isGlobalPath(toPath) ? window : obj, toPath, fromValue);
  569
+        Ember.trySetPath(Ember.isGlobalPath(toPath) ? ROOT : obj, toPath, fromValue);
568 570
       } else {
569 571
         Ember._suspendObserver(obj, toPath, this, this.toDidChange, function () {
570  
-          Ember.trySetPath(Ember.isGlobalPath(toPath) ? window : obj, toPath, fromValue);
  572
+          Ember.trySetPath(Ember.isGlobalPath(toPath) ? ROOT : obj, toPath, fromValue);
571 573
         });
572 574
       }
573 575
     // if we're synchronizing *to* the remote object
@@ -577,7 +579,7 @@ Binding.prototype = /** @scope Ember.Binding.prototype */ {
577 579
         Ember.Logger.log(' ', this.toString(), '<-', toValue, obj);
578 580
       }
579 581
       Ember._suspendObserver(obj, fromPath, this, this.fromDidChange, function () {
580  
-        Ember.trySetPath(Ember.isGlobalPath(fromPath) ? window : obj, fromPath, toValue);
  582
+        Ember.trySetPath(Ember.isGlobalPath(fromPath) ? ROOT : obj, fromPath, toValue);
581 583
       });
582 584
     }
583 585
   }
29  packages/ember-metal/lib/core.js
@@ -5,6 +5,8 @@
5 5
 // ==========================================================================
6 6
 /*globals Em:true ENV */
7 7
 
  8
+var ROOT;
  9
+
8 10
 if ('undefined' === typeof Ember) {
9 11
 /**
10 12
   @namespace
@@ -29,17 +31,24 @@ if ('undefined' === typeof Ember) {
29 31
 
30 32
 // Create core object. Make it act like an instance of Ember.Namespace so that
31 33
 // objects assigned to it are given a sane string representation.
32  
-Ember = { isNamespace: true, toString: function() { return "Ember"; } };
  34
+var Ember = { isNamespace: true, toString: function() { return "Ember"; } };
  35
+
  36
+ROOT = typeof window == 'undefined' ? typeof global == 'undefined' ? this : global : window;
33 37
 
34 38
 // aliases needed to keep minifiers from removing the global context
35  
-if ('undefined' !== typeof window) {
36  
-  window.Em = window.Ember = Em = Ember;
37  
-}
  39
+ROOT.Ember = ROOT.Em = Ember;
38 40
 
39 41
 }
40 42
 
41 43
 /**
42 44
   @static
  45
+  @type Object
  46
+  @constant
  47
+*/
  48
+Ember.ROOT = ROOT;
  49
+
  50
+/**
  51
+  @static
43 52
   @type String
44 53
   @default '0.9.7.1'
45 54
   @constant
@@ -160,15 +169,15 @@ Ember.K = function() { return this; };
160 169
 // Stub out the methods defined by the ember-debug package in case it's not loaded
161 170
 
162 171
 if ('undefined' === typeof ember_assert) {
163  
-  window.ember_assert = Ember.K;
  172
+  ROOT.ember_assert = Ember.K;
164 173
 }
165 174
 
166  
-if ('undefined' === typeof ember_warn) { window.ember_warn = Ember.K; }
  175
+if ('undefined' === typeof ember_warn) { ROOT.ember_warn = Ember.K; }
167 176
 
168  
-if ('undefined' === typeof ember_deprecate) { window.ember_deprecate = Ember.K; }
  177
+if ('undefined' === typeof ember_deprecate) { ROOT.ember_deprecate = Ember.K; }
169 178
 
170 179
 if ('undefined' === typeof ember_deprecateFunc) {
171  
-  window.ember_deprecateFunc = function(_, func) { return func; };
  180
+  ROOT.ember_deprecateFunc = function(_, func) { return func; };
172 181
 }
173 182
 
174 183
 // ..........................................................
@@ -178,7 +187,7 @@ if ('undefined' === typeof ember_deprecateFunc) {
178 187
 /**
179 188
   @class
180 189
 
181  
-  Inside Ember-Metal, simply uses the window.console object.
  190
+  Inside Ember-Metal, simply uses the ROOT.console object.
182 191
   Override this to provide more robust logging functionality.
183 192
 */
184  
-Ember.Logger = window.console || { log: Ember.K, warn: Ember.K, error: Ember.K };
  193
+Ember.Logger = ROOT.console || { log: Ember.K, warn: Ember.K, error: Ember.K };
13  packages/ember-metal/lib/mixin.js
@@ -15,6 +15,7 @@ require('ember-metal/binding');
15 15
 
16 16
 var Mixin, MixinDelegate, REQUIRED, Alias;
17 17
 var classToString, superClassString;
  18
+var ROOT = Ember.ROOT;
18 19
 
19 20
 var a_map = Ember.ArrayUtils.map;
20 21
 var a_indexOf = Ember.ArrayUtils.indexOf;
@@ -473,17 +474,17 @@ function findNamespaces() {
473 474
 
474 475
   if (Namespace.PROCESSED) { return; }
475 476
 
476  
-  for (var prop in window) {
477  
-    //  get(window.globalStorage, 'isNamespace') would try to read the storage for domain isNamespace and cause exception in Firefox.
  477
+  for (var prop in ROOT) {
  478
+    //  get(ROOT.globalStorage, 'isNamespace') would try to read the storage for domain isNamespace and cause exception in Firefox.
478 479
     // globalStorage is a storage obsoleted by the WhatWG storage specification. See https://developer.mozilla.org/en/DOM/Storage#globalStorage
479  
-    if (prop === "globalStorage" && window.StorageList && window.globalStorage instanceof window.StorageList) { continue; }
480  
-    // Unfortunately, some versions of IE don't support window.hasOwnProperty
481  
-    if (window.hasOwnProperty && !window.hasOwnProperty(prop)) { continue; }
  480
+    if (prop === "globalStorage" && ROOT.StorageList && ROOT.globalStorage instanceof ROOT.StorageList) { continue; }
  481
+    // Unfortunately, some versions of IE don't support ROOT.hasOwnProperty
  482
+    if (ROOT.hasOwnProperty && !ROOT.hasOwnProperty(prop)) { continue; }
482 483
 
483 484
     // At times we are not allowed to access certain properties for security reasons.
484 485
     // There are also times where even if we can access them, we are not allowed to access their properties.
485 486
     try {
486  
-      obj = window[prop];
  487
+      obj = ROOT[prop];
487 488
       isNamespace = obj && get(obj, 'isNamespace');
488 489
     } catch (e) {
489 490
       continue;
3  packages/ember-metal/lib/observer.js
@@ -14,6 +14,7 @@ var AFTER_OBSERVERS = ':change';
14 14
 var BEFORE_OBSERVERS = ':before';
15 15
 var guidFor = Ember.guidFor;
16 16
 var normalizePath = Ember.normalizePath;
  17
+var ROOT = Ember.ROOT;
17 18
 
18 19
 var deferred = 0;
19 20
 var array_Slice = Array.prototype.slice;
@@ -160,7 +161,7 @@ function xformForArgs(args) {
160 161
     var obj = params[0], keyName = changeKey(params[1]), val;
161 162
     var copy_args = args.slice();
162 163
     if (method.length>2) {
163  
-      val = Ember.getPath(Ember.isGlobalPath(keyName) ? window : obj, keyName);
  164
+      val = Ember.getPath(Ember.isGlobalPath(keyName) ? ROOT : obj, keyName);
164 165
     }
165 166
     copy_args.unshift(obj, keyName, val);
166 167
     method.apply(target, copy_args);
4  packages/ember-runtime/lib/core.js
@@ -8,14 +8,16 @@
8 8
 require('ember-metal');
9 9
 
10 10
 var indexOf = Ember.ArrayUtils.indexOf;
  11
+var ROOT = Ember.ROOT;
11 12
 
12 13
 // ........................................
13 14
 // GLOBAL CONSTANTS
14 15
 //
15 16
 
16 17
 // ensure no undefined errors in browsers where console doesn't exist
  18
+// can this be removed? it's in ember-metal/core.js
17 19
 if (typeof console === 'undefined') {
18  
-  window.console = {};
  20
+  ROOT.console = {};
19 21
   console.log = console.info = console.warn = console.error = function() {};
20 22
 }
21 23
 
4  packages/ember-runtime/lib/mixins/target_action_support.js
... ...
@@ -1,4 +1,4 @@
1  
-var get = Ember.get, set = Ember.set, getPath = Ember.getPath;
  1
+var get = Ember.get, set = Ember.set, getPath = Ember.getPath, ROOT = Ember.ROOT;
2 2
 
3 3
 Ember.TargetActionSupport = Ember.Mixin.create({
4 4
   target: null,
@@ -10,7 +10,7 @@ Ember.TargetActionSupport = Ember.Mixin.create({
10 10
     if (Ember.typeOf(target) === "string") {
11 11
       // TODO: Remove the false when deprecation is done
12 12
       var value = getPath(this, target, false);
13  
-      if (value === undefined) { value = getPath(window, target); }
  13
+      if (value === undefined) { value = getPath(ROOT, target); }
14 14
       return value;
15 15
     } else {
16 16
       return target;
4  packages/ember-runtime/lib/system/namespace.js
@@ -6,7 +6,7 @@
6 6
 
7 7
 require('ember-runtime/system/object');
8 8
 
9  
-var indexOf = Ember.ArrayUtils.indexOf;
  9
+var indexOf = Ember.ArrayUtils.indexOf, ROOT = Ember.ROOT;
10 10
 
11 11
 /**
12 12
   @private
@@ -36,7 +36,7 @@ Ember.Namespace = Ember.Object.extend({
36 36
 
37 37
   destroy: function() {
38 38
     var namespaces = Ember.Namespace.NAMESPACES;
39  
-    window[this.toString()] = undefined;
  39
+    ROOT[this.toString()] = undefined;
40 40
     namespaces.splice(indexOf(namespaces, this), 1);
41 41
     this._super();
42 42
   }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.