Last active
March 8, 2019 19:41
-
-
Save Duder-onomy/e75ffb478c7ccd40e4e97d92a0cbb3b1 to your computer and use it in GitHub Desktop.
Ember Refresh Model Hook on Tab Focus
This file contains hidden or 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
import Mixin from '@ember/object/mixin'; | |
import { inject as service } from '@ember/service'; | |
import { task, timeout } from 'ember-concurrency'; | |
export default Mixin.create({ | |
fastboot: service(), | |
unifiedEventHandler: service(), // ember-singularity | |
activate(...args) { | |
this._super(...args); | |
if (!this.fastboot.isFastBoot) { | |
this.performRefreshModelHook = this.performRefreshModelHook.bind(this); // because we need a function ref to unregister with | |
this.unifiedEventHandler.register('window', 'focus', this.performRefreshModelHook); | |
} | |
}, | |
deactivate(...args) { | |
if (!this.fastboot.isFastBoot) { | |
this.unifiedEventHandler.unregister('window', 'focus', this.performRefreshModelHook); | |
} | |
this._super(...args); | |
}, | |
performRefreshModelHook() { | |
this.refreshModelHook.perform(); | |
}, | |
refreshModelHook: task(function* () { | |
yield timeout(2000); | |
const model = yield this.model(this.paramsFor(this.routeName)); | |
this.setupController(this.controllerFor(this.routeName), model); | |
}).restartable(), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment