Skip to content

Instantly share code, notes, and snippets.

@pixelhandler
Last active July 25, 2018 05:40
Show Gist options
  • Save pixelhandler/987260f76ddfee5d8a4833ea7605258e to your computer and use it in GitHub Desktop.
Save pixelhandler/987260f76ddfee5d8a4833ea7605258e to your computer and use it in GitHub Desktop.
shared task in route hooks
import Ember from 'ember';
import { inject as service } from '@ember/service';
export default Ember.Controller.extend({
shared: service()
});
import EmberRouter from '@ember/routing/router';
import config from './config/environment';
const Router = EmberRouter.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('foo');
this.route('bar');
this.route('baz');
});
export default Router;
import Ember from 'ember';
import { get } from '@ember/object';
export default Ember.Route.extend({
shared: Ember.inject.service(),
async model() {
await get(this, 'shared').delay();
return get(this, 'shared.value');
}
});
import Ember from 'ember';
import { get, set } from '@ember/object';
import { Promise as EmberPromise } from 'rsvp';
export default Ember.Route.extend({
shared: Ember.inject.service(),
async model() {
let { value } = get(this, 'shared.value');
await EmberPromise.resolve({ value });
},
async afterModel() {
await get(this, 'shared').delay();
},
setupController(controller, model) {
this._super(controller, model);
set(controller, 'model', get(this, 'shared.value'));
}
});
import Ember from 'ember';
import { get } from '@ember/object';
export default Ember.Route.extend({
shared: Ember.inject.service(),
async beforeModel() {
await get(this, 'shared').delay();
},
model() {
return get(this, 'shared.value');
}
});
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
import { get, set } from '@ember/object';
import { reads } from '@ember/object/computed';
import { Promise as EmberPromise } from 'rsvp';
export default Ember.Service.extend({
delayTask: task(function*() {
yield timeout(1000);
return { value: `runs... ${get(this, 'delayTask.performCount')}` };
}).drop().cancelOn('deactivate').debug(),
async delay() {
let isRunning = get(this, 'delayTask.isRunning');
let hasRunOnce = get(this, 'delayTask.performCount') > 0;
if (isRunning && hasRunOnce) {
let { value } = get(this, 'value') || { value: null };
return await EmberPromise.resolve({ value });
}
return await get(this, 'delayTask').perform();
},
value: reads('delayTask.lastSuccessful.value')
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
a.active {
color: black;
}
<p>
Shared task state: {{shared.delayTask.state}}
</p>
<p>
Transition between the routes before the task finishes, models use shared state from a service's task.
</p>
<p>
Hit the links below like playing <em>whack-a-mole</em>.
</p>
{{#link-to "foo"}}Foo{{/link-to}} |
{{#link-to "bar"}}Bar{{/link-to}} |
{{#link-to "baz"}}Baz{{/link-to}}
<br>
{{outlet}}
<h1>Bar</h1>
<p>
{{model.value}}
</p>
<h1>Baz</h1>
<p>
{{model.value}}
</p>
<h1>Foo</h1>
<p>
{{model.value}}
</p>
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0",
"ember-concurrency": "0.8.19"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment