-
-
Save camskene/18cebd25e1026977052c16d8385fae2e to your computer and use it in GitHub Desktop.
Ember - Return Promise from Computed Property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
shouldRenderComponent: false, | |
actions: { | |
renderComponent() { | |
this.toggleProperty('shouldRenderComponent'); | |
}, | |
}, | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { task } from 'ember-concurrency'; | |
let Api = Ember.Object.extend({ | |
getItems() { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve(['John', 'Paul', 'George', 'Ringo']); | |
}, 1000); | |
}); | |
}, | |
}); | |
let api = Api.create(); | |
export default Ember.Component.extend({ | |
api, | |
isLoading: true, | |
init() { | |
this._super(...arguments); | |
this.get('fetchItems'); | |
this.get('eccItemsFetch').perform(); | |
}, | |
items: undefined, | |
fetchItems: Ember.computed(function() { | |
this.get('api').getItems().then((items) => { | |
this.setProperties({ | |
items, | |
isLoading: false, | |
}); | |
}); | |
}), | |
otherItems: Ember.computed(function() { | |
let data = Ember.Object.create({ | |
isLoading: true, | |
}); | |
this.get('api').getItems().then((items) => { | |
data.setProperties({ | |
items, | |
isLoading: false, | |
}); | |
}); | |
return data; | |
}), | |
eccItemsFetch: task(function* () { | |
let items = yield this.get('api').getItems(); | |
this.set('eccItems', items); | |
}), | |
eccItems: undefined, | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.15.0", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": true, | |
"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-concurrency": "0.8.21" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment