Skip to content

Commit

Permalink
Rebase 4343 (#4397)
Browse files Browse the repository at this point in the history
* [BUGFIX beta] Avoid using `Ember.lookup` to setup global.

`Ember.lookup` is an internal mechanism used by older versions of Ember
to "lookup" things on the global scope. In Ember 2.7 usage of
`Ember.lookup` will be deprecated, so this removes our need for it.

This also adds a deprecation when using the global `DS` (suggesting
imports instead).

* Explicitly name the namespace

Since the DS namespace is not added to Ember.lookup anymore, the name
for the namespace needs to be defined explicitly.

* Disable the window.DS warning in the bower build
  • Loading branch information
bmac committed May 25, 2016
1 parent 77dc781 commit fe07647
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
5 changes: 3 additions & 2 deletions addon/-private/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import VERSION from 'ember-data/version';
@type String
@static
*/
var DS = Ember.Namespace.create({
VERSION: VERSION
const DS = Ember.Namespace.create({
VERSION: VERSION,
name: "DS"
});

if (Ember.libraries) {
Expand Down
19 changes: 19 additions & 0 deletions addon/-private/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* globals global, window, self */

// originally from https://github.com/emberjs/ember.js/blob/c0bd26639f50efd6a03ee5b87035fd200e313b8e/packages/ember-environment/lib/global.js

// from lodash to catch fake globals
function checkGlobal(value) {
return (value && value.Object === Object) ? value : undefined;
}

// element ids can ruin global miss checks
function checkElementIdShadowing(value) {
return (value && value.nodeType === undefined) ? value : undefined;
}

// export real global
export default checkGlobal(checkElementIdShadowing(typeof global === 'object' && global)) ||
checkGlobal(typeof self === 'object' && self) ||
checkGlobal(typeof window === 'object' && window) ||
new Function('return this')(); // eval outside of strict mode
17 changes: 16 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Ember from "ember";
import { deprecate } from "ember-data/-private/debug";
import isEnabled from "ember-data/-private/features";
import global from "ember-data/-private/global";

/**
Ember Data
@module ember-data
Expand Down Expand Up @@ -163,6 +166,18 @@ Object.defineProperty(DS, 'normalizeModelName', {
value: normalizeModelName
});

Ember.lookup.DS = DS;
Object.defineProperty(global, 'DS', {
configurable: true,
get() {
deprecate(
'Using the global version of DS is deprecated. Please either import ' +
'the specific modules needed or `import DS from \'ember-data\';`.',
false,
{ id: 'ember-data.global-ds', until: '3.0.0' }
);

return DS;
}
});

export default DS;
9 changes: 9 additions & 0 deletions lib/ds-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;(function() {
var global = require('ember-data/-private/global').default;
var DS = require('ember-data').default;
Object.defineProperty(global, 'DS', {
get: function() {
return DS;
}
});
})();
4 changes: 3 additions & 1 deletion lib/javascripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function collapse(tree, outputFileName) {

var emberDataShimsPath = path.join(__dirname, 'ember-data-shims.js');
var emberDataShims = fs.readFileSync(emberDataShimsPath, { encoding: 'utf8' });
var dsGlobalPath = path.join(__dirname, 'ds-global.js');
var dsGlobal = fs.readFileSync(dsGlobalPath, { encoding: 'utf8' });
var emberDataInitialierPath = path.join(__dirname, '../tests/ember-data-initializers.js');
var emberDataInitialier = fs.readFileSync(emberDataInitialierPath, { encoding: 'utf8' });

Expand All @@ -52,7 +54,7 @@ function collapse(tree, outputFileName) {
inputFiles: ['license.js', 'loader.js', '**/*.js'],
outputFile: '/' + outputFileName,
header: '(function(){ \n"use strict";\n',
footer: '\nrequire("ember-data");\n})();\n' + emberDataShims + emberDataInitialier
footer: '\nrequire("ember-data");\n' + dsGlobal + '})();\n' + emberDataShims + emberDataInitialier
});
}

Expand Down

0 comments on commit fe07647

Please sign in to comment.