Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@seifsallam
Created February 18, 2014 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seifsallam/9077810 to your computer and use it in GitHub Desktop.
Save seifsallam/9077810 to your computer and use it in GitHub Desktop.
// A PromiseAction is an action set on the controller,
// but not in the actions hash, which allows it to be
// passed in directly to the component. The component
// just sees it as a function that it can call that
// returns a promise. This is nice for when the component
// needs to emit some action that can fail; if it fails
// it can clean up after itself.
// Example:
// export default Controller.extend({
// setSomeValueAsyncly: promiseAction(function(newValue) {
// this.set('something', newValue);
// // simulate a slow set...
// return new Ember.RSVP.Promise(function(resolve) {
// Ember.run.later(resolve, 1000);
// });
// })
// });
//
// {{some-component on-change=setSomeValueAsyncly}}
export default function promiseActionComputedProperty(fn) {
return Ember.computed(function() {
var self = this;
return function() {
var args = arguments;
return new Ember.RSVP.Promise(function(resolve) {
resolve(fn.apply(self, args));
});
};
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment