Skip to content

Instantly share code, notes, and snippets.

@feanor07
Last active April 20, 2017 14:04
Show Gist options
  • Save feanor07/f8c6c93f066faeeac783a1f59ca36ccf to your computer and use it in GitHub Desktop.
Save feanor07/f8c6c93f066faeeac783a1f59ca36ccf to your computer and use it in GitHub Desktop.
so#43512119
import Ember from 'ember';
export default Ember.Component.extend({
isLongJobRunning: Ember.computed('result', function(){
return this.get('result') === null;
}),
isLongJobNeverStarted: Ember.computed('result', function(){
return this.get('result') === undefined;
}),
isLongJobFinished: Ember.computed('result', function(){
return Ember.isPresent(this.get('result'));
}),
mockRemoteCallService: Ember.inject.service(),
actions: {
startLongJob() {
this.set('result', null); this.get('mockRemoteCallService').doLongJob().then((result)=>this.set('result', result));
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
});
import Ember from 'ember';
export default Ember.Service.extend({
updateProgress(percentage) {
this.set('percentage', `${100*percentage}%`);
},
doLongJob() {
this.updateProgress(0);
let that = this;
return new Ember.RSVP.Promise(
function (resolve, reject) {
function progress(count){
that.updateProgress(count/10);
}
let count = 0;
function timeout() {
window.setTimeout(function() {
count++;
if (count < 11) {
progress(count);
timeout();
} else {
resolve(`Data returned ${Math.random()}`);
}
}, 200);
}
timeout();
});
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
{{progress-displayer-component}}
<br>
<br>
{{yield}}
{{#if isLongJobRunning}}
The progress is at:{{mockRemoteCallService.percentage}}
{{/if}}
<br>
{{#unless isLongJobRunning}}
<button onclick={{action 'startLongJob'}}>Start New Long Job</button>
{{/unless}}
<br>
{{#if isLongJobFinished}}
Remote call result is: {{result}}
{{/if}}
{
"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