Skip to content

Commit

Permalink
[BUGFIX Release] Clear cached instances when factories are unregistered.
Browse files Browse the repository at this point in the history
`ApplicationInstance#unregister` now overrides
`RegistryProxy#unregister` in order to clear any cached instances of the
unregistered factory (via `__container__.reset()`).

[Closes #11173]
(cherry picked from commit 8768e67)
  • Loading branch information
dgeb authored and rwjblue committed Dec 4, 2015
1 parent 5928fb7 commit 53267df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/ember-application/lib/system/application-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,21 @@ let ApplicationInstance = EmberObject.extend(RegistryProxy, ContainerProxy, {
willDestroy() {
this._super(...arguments);
run(this.__container__, 'destroy');
},

/**
Unregister a factory.
Overrides `RegistryProxy#unregister` in order to clear any cached instances
of the unregistered factory.
@public
@method unregister
@param {String} fullName
*/
unregister(fullName) {
this.__container__.reset(fullName);
this._super(...arguments);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Application from 'ember-application/system/application';
import ApplicationInstance from 'ember-application/system/application-instance';
import run from 'ember-metal/run_loop';
import jQuery from 'ember-views/system/jquery';
import factory from 'container/tests/test-helpers/factory';

let app, appInstance;

Expand Down Expand Up @@ -124,3 +125,26 @@ QUnit.test('customEvents added to the application instance before setupEventDisp

appInstance.setupEventDispatcher();
});

QUnit.test('unregistering a factory clears all cached instances of that factory', function(assert) {
assert.expect(3);

run(function() {
appInstance = ApplicationInstance.create({ application: app });
});

let PostController = factory();

appInstance.register('controller:post', PostController);

let postController1 = appInstance.lookup('controller:post');
assert.ok(postController1, 'lookup creates instance');

appInstance.unregister('controller:post');
appInstance.register('controller:post', PostController);

let postController2 = appInstance.lookup('controller:post');
assert.ok(postController2, 'lookup creates instance');

assert.notStrictEqual(postController1, postController2, 'lookup creates a brand new instance, because previous one was reset');
});

0 comments on commit 53267df

Please sign in to comment.