mirror of
https://github.com/status-im/consul.git
synced 2025-01-23 03:59:18 +00:00
ui: Adds controller lifecycle reset
hook (#5056)
This commit is contained in:
parent
2920f73ddd
commit
c71f718bc7
23
ui-v2/app/initializers/controller-lifecycle.js
Normal file
23
ui-v2/app/initializers/controller-lifecycle.js
Normal file
@ -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,
|
||||||
|
};
|
@ -1,3 +1,4 @@
|
|||||||
|
import Controller from '@ember/controller';
|
||||||
import Component from '@ember/component';
|
import Component from '@ember/component';
|
||||||
import Mixin from '@ember/object/mixin';
|
import Mixin from '@ember/object/mixin';
|
||||||
import { inject as service } from '@ember/service';
|
import { inject as service } from '@ember/service';
|
||||||
@ -8,15 +9,23 @@ export default Mixin.create({
|
|||||||
init: function() {
|
init: function() {
|
||||||
this._super(...arguments);
|
this._super(...arguments);
|
||||||
this._listeners = get(this, 'dom').listeners();
|
this._listeners = get(this, 'dom').listeners();
|
||||||
let method = 'willDestroy';
|
let teardown = ['willDestroy'];
|
||||||
if (this instanceof Component) {
|
if (this instanceof Component) {
|
||||||
method = 'willDestroyElement';
|
teardown = ['willDestroyElement'];
|
||||||
|
} else if (this instanceof Controller) {
|
||||||
|
if (typeof this.reset === 'function') {
|
||||||
|
teardown.push('reset');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
teardown.forEach(method => {
|
||||||
const destroy = this[method];
|
const destroy = this[method];
|
||||||
this[method] = function() {
|
this[method] = function() {
|
||||||
destroy(...arguments);
|
if (typeof destroy === 'function') {
|
||||||
|
destroy.apply(this, arguments);
|
||||||
|
}
|
||||||
this.removeListeners();
|
this.removeListeners();
|
||||||
};
|
};
|
||||||
|
});
|
||||||
},
|
},
|
||||||
listen: function(target, event, handler) {
|
listen: function(target, event, handler) {
|
||||||
return this._listeners.add(...arguments);
|
return this._listeners.add(...arguments);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user