how can i return some value from actions?? how action must called to accomplish this issue??
vat r = this.send("someAction", params);
...
actions:{
someAction: function(){
return "someValue";
}
}
how can i return some value from actions?? how action must called to accomplish this issue??
vat r = this.send("someAction", params);
...
actions:{
someAction: function(){
return "someValue";
}
}
Actions are one way, call the function directly off the target object (controller, router, plain object) if you want a return value.
How about in the case where you want to test an action,say in QUnit?
Call the action just like you would any other function.
this.get('targetObject.actions.someFunction')();
Iāve always wondered this same thing, because what I wanted to return from an action was a promise. Actions on the router have definitely made my application very elegant, but Iāve found not being able to return a promise has always been an annoyance in a few places. Why not allow an action return a promise?
Andrew
I believe my previous suggestion is now obsolete, as actions are āprivateā now.
As for actions returning promises, Iām not sure what you mean. Actions are oneway, can you post an example of what youāre trying to achieve?
Could you perhaps create a deferred, hook up to the promise of that deferred, and then resolve / reject it in the actionHandler?
Pseudo code:
triggersAction: function {
Ā var doneWithAsync = Ember.RSVP.defer(),
Ā Ā Ā Ā Ā doTaskWithAsyncResult = Ember.K;
Ā doneWithAsync.promise
Ā Ā Ā .then(doTaskWithAsyncResult);
Ā this.sendAction('doAsynchronously', doneWithAsync)
},
actions: {
Ā doAsynronously: function(deferred) {
Ā Ā Ā $.getJSON('/my/json').then(function() {
Ā Ā Ā Ā Ā deferred.resolve('foo');Ā Ā Ā Ā
Ā Ā Ā })
Ā }
}
Not entirely sure why youād do it this way instead of calling a method directly though ā¦
First, thanks for returning to discuss this and my apologies that all the use cases are not that fresh on my mind. I started working with ember before we had the async router, so some of my concerns may stem from problems I had back then when I did some pretty horrible things to deal with the lack of knowing when a route had finished transitioning. I think transitionTo on a router now returns some sort of object you can trigger on but itās not as important with all the various hooks available in the router.
The case I can think of right now where a return value on a router action would be most useful has to do with how Iām handling modals. In my application, I have a stack of modals and I generate modals by sending a message to create modal of a particular type for a particular object. (The stack is just a CollectionView of modal views). I can close the top modal by sending ādismissā event through the routing stack. I started using modals because I didnāt want to describe all the possible transitions at every route, and at least at the time, I thought the overhead of doing true routing was too heavy. However, I did want the pushstate behavior (i.e. I wanted to be able to use the back button to close the modal). This all works great but the other day I wanted to programmatically open a new modal right after closing another modal. So I would need to somehow trigger on the popstate following the dismiss. If my ādismissā event returned a promise, I think I could do this pretty elegantly. Iām sure I could pass a handler, but returning promises looks much cleaner.