Return value of actions


#1

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";
      }    
    }

#2

Actions are one way, call the function directly off the target object (controller, router, plain object) if you want a return value.


#3

How about in the case where you want to test an action,say in QUnit?


#4

Call the action just like you would any other function.

this.get('targetObject.actions.someFunction')();


#5

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


#6

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?


#7

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 …


#8

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.