【发布时间】:2021-03-29 09:06:46
【问题描述】:
在我的控制器中,我有一堆函数存储在一个对象(使用 IIFE)中,如下所示:
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));
};
};
}
})();
在我的 XML 视图中,我想在不同的情况下部分应用排序功能,例如:
<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 -->
如您所见,函数首先需要一个布尔值,然后是事件。我想我可以在 XML-View 中部分应用布尔值,然后在按下按钮时使用事件调用事件返回函数。
如果我这样做,事件不会传递给函数。当我按下按钮时,给出的是布尔值而不是事件。
如何在加载 XMLView 时首先使用布尔值调用函数 onBar/onFoo,然后在按下按钮时使用事件调用返回的函数?
【问题讨论】:
-
感谢@Marc 完美运行
标签: sapui5