-
-
Save vitch/2df2389667e0f60341c11bf709e8cbbb 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'; | |
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'); | |
}, | |
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; | |
}), | |
}); |
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": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment