How to return values ​​from actions in emberjs

How to return some value from actions? I tried this:

var t = this.send("someAction", params);

...

    actions:{
      someAction: function(){
          return "someValue";
      }    
    }
+4
source share
4 answers

actions do not return values, only true / false / undefined to allow duplication. define function.

Ember Code:

  send: function(actionName) {
    var args = [].slice.call(arguments, 1), target;

    if (this._actions && this._actions[actionName]) {
      if (this._actions[actionName].apply(this, args) === true) {
        // handler returned true, so this action will bubble
      } else {
        return;
      }
    } else if (this.deprecatedSend && this.deprecatedSendHandles && this.deprecatedSendHandles(actionName)) {
      if (this.deprecatedSend.apply(this, [].slice.call(arguments)) === true) {
        // handler return true, so this action will bubble
      } else {
        return;
      }
    }

    if (target = get(this, 'target')) {
      Ember.assert("The `target` for " + this + " (" + target + ") does not have a `send` method", typeof target.send === 'function');
      target.send.apply(target, arguments);
    }
  }
+3
source

Try

var t = this.send("someAction", params);

instead

vat r = this.send("someAction", params);
+1
source

. , .

, , , .

App.Controller = Ember.Controller.extend({
    functionToReturnValue: function(param1, param2) {
        // do some calculation
        return value;
    },
});

:

var value = this.get("functionToReturnValue").call(this, param1, param2);

:

var controller = this.get("controller"); // from view, [needs] or whatever

var value = controller.get("functionToReturnValue").call(controller, param1, param2); // from other controller

call() , ; this. this. , , .

API, , , : http://emberjs.com/api/#method_tryInvoke

+1

@set ,

actions:{
  someAction: function(){
    //  return "someValue";
    this.set('var', someValue);
  }    
}
0

All Articles