From b46db82dd445b5cd4c596abd020df9d7c500c85a Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Tue, 6 Dec 2016 12:14:58 -0500 Subject: [PATCH] Make into a true polyfill Fixes #5. Why not patch in an initializer? Because then it wouldn't work in places like unit and integration tests. --- addon/index.js | 4 ++++ index.js | 22 +++++++++++++++++++++- tests/unit/getowner-test.js | 2 +- vendor/install-getowner-polyfill.js | 13 +++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 vendor/install-getowner-polyfill.js diff --git a/addon/index.js b/addon/index.js index fd60ff3..ce8796d 100644 --- a/addon/index.js +++ b/addon/index.js @@ -3,6 +3,10 @@ import FakeOwner from './fake-owner'; let hasGetOwner = !!Ember.getOwner; +Ember.deprecate("ember-getowner-polyfill is now a true polyfill. Use Ember.getOwner directly instead of importing from ember-getowner-polyfill", false, { + id: "ember-getowner-polyfill.import" +}); + export default function(object) { let owner; diff --git a/index.js b/index.js index a2dac7d..45b208b 100644 --- a/index.js +++ b/index.js @@ -2,5 +2,25 @@ 'use strict'; module.exports = { - name: 'ember-getowner-polyfill' + name: 'ember-getowner-polyfill', + included: function() { + this._ensureThisImport(); + this.import('vendor/install-getowner-polyfill.js'); + }, + _ensureThisImport: function() { + if (!this.import) { + this._findHost = function findHostShim() { + var current = this; + var app; + do { + app = current.app || app; + } while (current.parent.parent && (current = current.parent)); + return app; + }; + this.import = function importShim(asset, options) { + var app = this._findHost(); + app.import(asset, options); + }; + } + } }; diff --git a/tests/unit/getowner-test.js b/tests/unit/getowner-test.js index 11c2a61..8ade49f 100644 --- a/tests/unit/getowner-test.js +++ b/tests/unit/getowner-test.js @@ -1,6 +1,6 @@ import Ember from 'ember'; -import getOwner from 'ember-getowner-polyfill'; import { moduleFor, test } from 'ember-qunit'; +const { getOwner } = Ember; moduleFor('foo:bar', { beforeEach() { diff --git a/vendor/install-getowner-polyfill.js b/vendor/install-getowner-polyfill.js new file mode 100644 index 0000000..99315ac --- /dev/null +++ b/vendor/install-getowner-polyfill.js @@ -0,0 +1,13 @@ +var Ember = require('ember').default; +if (!Ember.getOwner) { + Object.defineProperty(Ember, 'getOwner', { + get: function() { + var FakeOwner = require('ember-getowner-polyfill/fake-owner').default; + return function(object) { + if (object.container) { + return new FakeOwner(object); + } + }; + } + }); +}