Ember comes with a built in helper that will provide computed properties for the status of an asynchronous task.
Ember.Object
and cannot be applied to an Ember.Component
directly.content
value.import Ember from 'ember';
const {
Component, PromiseProxyMixin, get, set, computed,
isPresent, run, RSVP: { Promise }
} = Ember;
const MyPromiseProxy = Ember.Object.extend(PromiseProxyMixin);
export default Component({
myProxy: computed('promise', {
get() {
const promise = get(this, 'promise');
return isPresent(promise) ? MyPromiseProxy.create({promise}) : null;
}
}),
actions: {
performTask() {
const fakeTask = new Promise((resolve) => {
run.later(resolve, 'Foobar', 2000);
});
set(this, 'promise', fakeTask);
}
}
});
{{#if myProxy.isPending}}
Loading…
{{else}}
<button onclick={{action "performTask"}}>
Start Task
</button>
{{/if}}
{{#if myProxy.isFulfilled}}
Done. {{myProxy.content}}
{{/if}}
{{#if myProxy.isRejected}}
Something went wrong. {{myProxy.reason}}
{{/if}}