【问题标题】:sapui5: Partial application of function in XML-Viewsapui5:XML-View中函数的部分应用
【发布时间】: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,然后在按下按钮时使用事件调用返回的函数?

【问题讨论】:

标签: sapui5


【解决方案1】:

正如 Marc 建议的正确答案的正确链接,如果您希望将事件作为从 XML 视图发送的参数,您需要指定它,否则它会被其他参数覆盖。

这意味着您的代码将如下所示:

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

这样您将作为第一个参数传递事件,并将布尔值作为第二个参数。

此功能的文档可在here 获得。

【讨论】:

  • 我建议关闭这个问题,因为它已经存在。你把我的评论变成你的答案不是我的本意..
  • 对不起,我只是想给出一个更详细的答案...如果打扰到您,我很抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多