Created
February 18, 2014 19:15
-
-
Save seifsallam/9077810 to your computer and use it in GitHub Desktop.
Based on my question here — http://discuss.emberjs.com/t/sendaction-as-a-promise/3143/5
This file contains 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
// 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