Skip to content

Commit

Permalink
Merge pull request #12680 from dgeb/reset-on-unregister
Browse files Browse the repository at this point in the history
[BUGFIX Release] Clear cached instances when factories are unregistered.
  • Loading branch information
rwjblue committed Dec 3, 2015
2 parents 3d1cf7b + 8768e67 commit 3b2ac4c
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 3b2ac4c

Please sign in to comment.