0

In my controller I have a bunch of functions stored in an object (using an IIFE) somethnig like this:

obj: (function() {
  return {
    onFoo: helper(x => x*2),
    onBar: helper(x => x + 1)
  };

  function helper(fn) { // fn is applied right above
    return bool => { // bool should be applied when XML view is loaded
      return oEvent => { // oEvent should be applied when button is pressed
        const i = parseInt(oEvent.getSource().getText());

        if (bool) 
          console.log(1 + fn(i));
        else 
          console.log(1 - fn(i));
      };
    };
  }
})();

In my XML-View I would like to partially apply the functions of sort in different circumstances e.g.:

<Button text="1" press=".obj.onFoo(true)" /> <!-- log 1 + 1*2 = 3 -->
<Button text="1" press=".obj.onFoo(false)" /> <!-- log 1 - 1*2 = -1 -->
<Button text="2" press=".obj.onBar(true)" /> <!-- log 1 + 2*2 = 5 -->
<Button text="2" press=".obj.onFoo(false)" /> <!-- log 1 - 2*2 = -3 -->

As you can see, the functions firstly expect a boolean and then the event. I thought I could partially apply the boolean in the XML-View and then the event returned function will be called with the event when the button is pressed.

If I do it like this the event is not passed to the function. When I press the button the boolean is given instead of the event.

How can I at first call the functions onBar/onFoo with a boolean when the XMLView is loaded and then call the returned function with the event when the button is pressed?

IceRevenge
  • 1,451
  • 18
  • 33
  • 1
    Does this answer your question? [How to pass parameters to a event handlerr in XML Views SAP UI5](https://stackoverflow.com/questions/25175631/how-to-pass-parameters-to-a-event-handlerr-in-xml-views-sap-ui5) – Marc Mar 29 '21 at 17:51
  • Thanks @Marc works perfectly – IceRevenge Mar 30 '21 at 15:17

1 Answers1

0

As Marc suggested with the right link to a correct answer, if you want to have the event as a parameter sent from the XML view you need to specify it, otherwise it gets overwritten by the other parameters.

This means your code would look like:

<Button text="1" press=".obj.onFoo($event, true)" />

This way you will pass as first parameter the event, and a boolean as second parameter.

The documentation for this feature is available here.

  • 1
    I suggested to close this question because it already exists. That you converted my comment into your answer was not my intention.. – Marc Apr 02 '21 at 19:21
  • Sorry, I was just trying to give a more detailed answer... I am sorry if it bothered you. – davidgbarbero Apr 06 '21 at 14:33