0
myapp.Calc.SkipOrder_Tap_canExecute = function (screen) {
    var enabledBool = new Boolean;
    screen.getOrders().then(function (result) {
        enabledBool = (screen.OrderPosition < result.data.length - 1);
    });
    return enabledBool;
};

The SkipOrder_Tap is a button object in MS LightSwitch. The problem is, I need to return the Boolean AFTER the asynchronous call.

borkith
  • 57
  • 2
  • 7
  • 1
    possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Felix Kling Mar 08 '14 at 18:23
  • You are almost there. You just have to return the promise from the function, and then call it as `myapp.Calc.FirstOrder_Tap_canExecute().then(function(enabled) { ...});`. Learn more about `.then`: http://api.jquery.com/deferred.then/ – Felix Kling Mar 08 '14 at 18:25
  • I updated the code to show usage of the getOrders result – borkith Mar 08 '14 at 18:44

1 Answers1

2

The idea of the promises is to handle asynchronous tasks.

I guess that screen.getOrders() makes asynchronous request and returns a promise. When you use then and pass a callback you will get the orders once the promise is resolved i.e. when your callback passed to then is invoked.

You can proceed as follows, using the promises "chaining":

myapp.Calc.FirstOrder_Tap_canExecute = function (screen) {
  return screen.getOrders().then(function (result) {
    return (screen.OrderPosition != 0);
  });
};

myapp.Calc.FirstOrder_Tap_canExecute()
.then(function (booleanResult) {
   //do whatever you need
});
Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
  • I see what you mean! I'm using MS LightSwitch and the FirstOrder_Tap is a button object. If I were to put "return false;" as the only statement in the canExecute function, the button is disabled. I don't see why your edit doesn't work, but the button is active :( – borkith Mar 08 '14 at 18:41
  • @borkith: If you imply that `myapp.Calc.SkipOrder_Tap_canExecute` should be a synchronous function, than that just wouldn't work. – Felix Kling Mar 08 '14 at 18:46
  • I updated the code to show that the "result" is the fulfilled promise data from getOrders – borkith Mar 08 '14 at 18:48
  • I tried "var orders = screen.getOrders();" but it doesn't return the .data I get from "screen.getOrders().then(function (result) {})". I also don't call the canExecute(), it's done by LightSwitch. – borkith Mar 08 '14 at 19:10