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?