Skip to content

Instantly share code, notes, and snippets.

@Duder-onomy
Last active March 8, 2019 19:41
Show Gist options
  • Save Duder-onomy/e75ffb478c7ccd40e4e97d92a0cbb3b1 to your computer and use it in GitHub Desktop.
Save Duder-onomy/e75ffb478c7ccd40e4e97d92a0cbb3b1 to your computer and use it in GitHub Desktop.
Ember Refresh Model Hook on Tab Focus
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