Skip to content

Instantly share code, notes, and snippets.

@fhrbek
Last active March 28, 2017 09:10
Show Gist options
  • Save fhrbek/6b965f0c3b6c6d56b93b065fbeff039a to your computer and use it in GitHub Desktop.
Save fhrbek/6b965f0c3b6c6d56b93b065fbeff039a to your computer and use it in GitHub Desktop.
Slow Route
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Randomly Slow Route Demo'
});
import Ember from 'ember';
export default Ember.Controller.extend({
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('slow-route', {path: '/'});
});
export default Router;
import Ember from 'ember';
const API = [
{
data: [1, 2, 3],
responseTime: 100
}, {
data: [100, 200, 300],
responseTime: 1000
}, {
data: [1000, 2000, 3000],
responseTime: 500
}];
export default Ember.Route.extend({
dataVersion: 0,
model() {
return new Promise((resolve, reject) => {
let dataVersion = this.get('dataVersion');
console.log('REFRESH DATA WITH VERSION', dataVersion, ' (IT WILL TAKE', API[dataVersion].responseTime, 'ms)');
Ember.run.later(function () {
let data = API[dataVersion].data;
console.log('DATA VERSION', dataVersion, 'RETRIEVED: ', data);
resolve(data);
}, API[dataVersion].responseTime);
});
},
setupController(controller, model) {
console.log('SETUP CONTROLLER WITH DATA', model);
this._super(controller, model);
},
actions: {
refresh() {
this.set('dataVersion', 1);
this.refresh();
Ember.run.later(() => {
this.set('dataVersion', 2);
this.refresh();
}, 100);
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<div>This demo shows how Ember handles (properly) data retrieval concurrency. Here's what you can do:</div>
<ul>
<li>Load the page - you should see items 1, 2, 3</li>
<li>Open browser console (in developer tools) to see debugging messages</li>
<li>Click the Refresh button - you should see items 1000, 2000, 3000 even though obsolete items 100, 200, 300 are retrieved later</li>
<li>Check the console - you should see that after obsolete data retrieval setupController is not called (which is correct)</li>
</ul>
<br>
{{outlet}}
<br>
<br>
<button {{action "refresh"}}>Refresh</button>
<hr>
<div>
<span>Items:</span>
<ul>
{{#each content as |item|}}
<li>{{item}}</li>
{{/each}}
</ul>
</div>
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment