Use custom global function in Expression Binding

℡╲_俬逩灬. 提交于 2019-12-24 14:46:20

问题


I see how to use expression binding.

https://sapui5.netweaver.ondemand.com/docs/guide/daf6852a04b44d118963968a1239d2c0.html

I can use a small range of function call (You can use functions that are available via global symbols, such as Math.max(...) or isNaN(...).)

I have my custom function isVisible(sParam) but I want write the sParam value directly into the xml code (sParams is never in the model), something like this

visible='{=isVisible('01 03 05 06')}'

My idea is to define isVisible as a global function but in debug the program don't cross the function code

window.isVisible = function (sParam) {
    ...
};

This is the dual Question of Text string as params in a formatter function


回答1:


There are only specific global objects and functions allowed. You can see a list in the sourcecode which should be the list of global symbols found in the documentation link you provided.

But I just had an idea:

Member access operator with the . operator

You can create a 'utility' JSONModel that contains functions. A JSONModel just takes any javascript object and provides access via binding path and databinding to it. So you access the root object via / and call a function on the resulting object:

onInit:function(){
  var utility = {
    isEven: function(x){
      return x % 2 === 0;
    }
  };
  this.getView().setModel(new JSONModel(utility), "utility");
}
<Button text="Hello 1" visible="{:= ${utility>/}.isEven(1) }"/>
<Button text="Hello 2" visible="{:= ${utility>/}.isEven(2) }"/>

You can even access the controller via closure.

Of course you can define that utility model at a higher level (at Component or even at the Core). You can have many utility models and you can mix models in an expression: visible="{= ${utility>/}.doSomething(${bla>/blub}, 42) } (not tried that).

See Plunker for an example



来源:https://stackoverflow.com/questions/37484559/use-custom-global-function-in-expression-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!