sapui5: XML-View中函数的部分应用
sapui5: Partial application of function in XML-View
在我的控制器中,我有一堆函数存储在一个对象中(使用 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-View 中,我想在不同情况下部分应用排序功能,例如:
<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 对正确答案 link 所建议的那样,如果您想将事件作为参数从 XML 视图发送,您需要指定它,否则它会被覆盖通过其他参数。
这意味着您的代码如下所示:
<Button text="1" press=".obj.onFoo($event, true)" />
这样,您将把事件作为第一个参数传递,将布尔值作为第二个参数传递。
此功能的文档可用 here。
在我的控制器中,我有一堆函数存储在一个对象中(使用 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-View 中,我想在不同情况下部分应用排序功能,例如:
<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 对正确答案 link 所建议的那样,如果您想将事件作为参数从 XML 视图发送,您需要指定它,否则它会被覆盖通过其他参数。
这意味着您的代码如下所示:
<Button text="1" press=".obj.onFoo($event, true)" />
这样,您将把事件作为第一个参数传递,将布尔值作为第二个参数传递。
此功能的文档可用 here。