Skip to content
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

UI: Adds controller lifecycle reset hook #5056

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions ui-v2/app/initializers/controller-lifecycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Route from '@ember/routing/route';
/**
* This initializer is very similar to:
* https://github.com/kellyselden/ember-controller-lifecycle
*
* Why is this included here:
* 1. Make sure lifecycle functions are functions, not just truthy.
* 2. Right now we don't want a setup function (at least until we are definitely decided that we want one)
* This is possibly a very personal opinion so it makes sense to just include this file here.
*/
Route.reopen({
resetController(controller, exiting, transition) {
this._super(...arguments);
if (typeof controller.reset === 'function') {
controller.reset(exiting);
}
},
});
export function initialize() {}

export default {
initialize,
};
23 changes: 16 additions & 7 deletions ui-v2/app/mixins/with-listeners.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Controller from '@ember/controller';
import Component from '@ember/component';
import Mixin from '@ember/object/mixin';
import { inject as service } from '@ember/service';
Expand All @@ -8,15 +9,23 @@ export default Mixin.create({
init: function() {
this._super(...arguments);
this._listeners = get(this, 'dom').listeners();
let method = 'willDestroy';
let teardown = ['willDestroy'];
if (this instanceof Component) {
method = 'willDestroyElement';
teardown = ['willDestroyElement'];
} else if (this instanceof Controller) {
if (typeof this.reset === 'function') {
teardown.push('reset');
}
}
const destroy = this[method];
this[method] = function() {
destroy(...arguments);
this.removeListeners();
};
teardown.forEach(method => {
const destroy = this[method];
this[method] = function() {
if (typeof destroy === 'function') {
destroy.apply(this, arguments);
}
this.removeListeners();
};
});
},
listen: function(target, event, handler) {
return this._listeners.add(...arguments);
Expand Down