-
-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use Application.create().buildInstance()
if possible.
#237
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
var __application__; | ||
|
||
export function setApplication(application) { | ||
__application__ = application; | ||
} | ||
|
||
export function getApplication() { | ||
return __application__; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,18 @@ | ||
/* globals requirejs */ | ||
import global from './global'; | ||
import { Promise } from 'rsvp'; | ||
|
||
import ApplicationInstance from '@ember/application/instance'; | ||
import Application from '@ember/application'; | ||
import EmberObject from '@ember/object'; | ||
import legacyBuildRegistry from './legacy-0-6-x/build-registry'; | ||
|
||
import require from 'require'; | ||
import Ember from 'ember'; | ||
import { getResolver } from './resolver'; | ||
|
||
const Owner = (function() { | ||
// this module only supports Ember 2.4+ but is evaluated | ||
// on older Ember versions (which we support via the legacy-0-6 API) | ||
// and calling `.extend` with undefined is an issue | ||
if (Ember._RegistryProxyMixin && Ember._ContainerProxyMixin) { | ||
return EmberObject.extend(Ember._RegistryProxyMixin, Ember._ContainerProxyMixin); | ||
export default function({ application, resolver }) { | ||
if (application) { | ||
return application.boot().then(app => app.buildInstance().boot()); | ||
} | ||
|
||
return EmberObject.extend(); | ||
})(); | ||
|
||
// different than the legacy-0-6-x version (build-registry.js) | ||
// in the following ways: | ||
// | ||
// * fewer fallbacks (supports only Ember 2.4+) | ||
// * returns "owner" (instead of a POJO with registry/container) | ||
// * directly calls getResolver if one was not provided | ||
export default function(resolver = getResolver()) { | ||
let fallbackRegistry, registry, container; | ||
let namespace = EmberObject.create({ | ||
Resolver: { | ||
create() { | ||
return resolver; | ||
}, | ||
}, | ||
}); | ||
|
||
function register(name, factory) { | ||
let thingToRegisterWith = registry || container; | ||
let existingFactory = container.factoryFor | ||
? container.factoryFor(name) | ||
: container.lookupFactory(name); | ||
|
||
if (!existingFactory) { | ||
thingToRegisterWith.register(name, factory); | ||
} | ||
} | ||
|
||
fallbackRegistry = Application.buildRegistry(namespace); | ||
fallbackRegistry.register('component-lookup:main', Ember.ComponentLookup); | ||
|
||
registry = new Ember.Registry({ | ||
fallback: fallbackRegistry, | ||
}); | ||
|
||
// Ember.ApplicationInstance was exposed in Ember 2.8 | ||
if (ApplicationInstance && ApplicationInstance.setupRegistry) { | ||
ApplicationInstance.setupRegistry(registry); | ||
} | ||
|
||
// these properties are set on the fallback registry by `buildRegistry` | ||
// and on the primary registry within the ApplicationInstance constructor | ||
// but we need to manually recreate them since ApplicationInstance's are not | ||
// exposed externally | ||
registry.normalizeFullName = fallbackRegistry.normalizeFullName; | ||
registry.makeToString = fallbackRegistry.makeToString; | ||
registry.describe = fallbackRegistry.describe; | ||
|
||
let owner = Owner.create({ | ||
__registry__: registry, | ||
__container__: null, | ||
}); | ||
|
||
container = registry.container({ owner: owner }); | ||
owner.__container__ = container; | ||
|
||
// TODO: this manual Ember Data setup should be removed in favor of | ||
// running the applications `initializers` to ensure the container is | ||
// properly setup, this would only need to be done once per test suite | ||
if ( | ||
(typeof require.has === 'function' && require.has('ember-data/setup-container')) || | ||
requirejs.entries['ember-data/setup-container'] | ||
) { | ||
// ember-data is a proper ember-cli addon since 2.3; if no 'import | ||
// 'ember-data'' is present somewhere in the tests, there is also no `DS` | ||
// available on the globalContext and hence ember-data wouldn't be setup | ||
// correctly for the tests; that's why we import and call setupContainer | ||
// here; also see https://github.com/emberjs/data/issues/4071 for context | ||
let setupContainer = require('ember-data/setup-container')['default']; | ||
setupContainer(registry); | ||
} else if (global.DS) { | ||
let DS = global.DS; | ||
if (DS._setupContainer) { | ||
DS._setupContainer(registry); | ||
} else { | ||
register('transform:boolean', DS.BooleanTransform); | ||
register('transform:date', DS.DateTransform); | ||
register('transform:number', DS.NumberTransform); | ||
register('transform:string', DS.StringTransform); | ||
register('serializer:-default', DS.JSONSerializer); | ||
register('serializer:-rest', DS.RESTSerializer); | ||
register('adapter:-rest', DS.RESTAdapter); | ||
} | ||
if (!resolver) { | ||
throw new Error( | ||
'You must set up the ember-test-helpers environment with either `setResolver` or `setApplication` before running any tests.' | ||
); | ||
} | ||
|
||
return owner; | ||
let { owner } = legacyBuildRegistry(resolver); | ||
return Promise.resolve(owner); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
import Resolver from 'ember-resolver'; | ||
|
||
export default Resolver; | ||
export let registry = Object.create(null); | ||
export function setRegistry(newRegistry) { | ||
registry = newRegistry; | ||
} | ||
|
||
export default Resolver.extend({ | ||
resolve(fullName) { | ||
return registry[fullName] || this._super(...arguments); | ||
}, | ||
}); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is that going to be required for all apps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, absolutely not! This is only a thing needed to test things internally here. Basically the
setResolverRegistry
test helper function (just here in the tests of this repo) resets the list of "known" modules so that we can test things likethis.owner.register
"wins" over what is resolved, and whatnot.