在表达式绑定中使用自定义全局函数 [英] Use custom global function in Expression Binding
问题描述
我知道如何使用表达式绑定.
I see how to use expression binding.
https://sapui5.netweaver.ondemand.com/docs/guide/daf6852a04b44d118963968a1239d2c0.html
我可以使用小范围的函数调用(您可以使用通过全局符号可用的函数,例如 Math.max(...) 或 isNaN(...).)
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(...).)
我有我的自定义函数 isVisible(sParam)
但我想将 sParam 值直接写入 xml 代码(sParams 永远不会在模型中),像这样
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')}'
我的想法是将 isVisible 定义为全局函数,但在调试时程序不要跨越函数代码
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) {
...
};
推荐答案
只允许特定的全局对象和函数.您可以在 源代码,它应该是您提供的文档链接中的全局符号列表.
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.
但我只是有一个想法:
成员访问运算符,带有 .操作员
Member access operator with the . operator
您可以创建一个包含函数的实用程序"JSONModel
.JSONModel
只接受任何 javascript 对象,并通过绑定路径和数据绑定提供访问.所以你通过 /
访问根对象并在结果对象上调用一个函数:
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.
当然,您可以在更高级别(在Component
或什至在Core
)定义该实用程序模型.您可以拥有多个实用模型,也可以在表达式中混合模型:visible="{= ${utility>/}.doSomething(${bla>/blub}, 42) }
(未尝试那个).
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).
参见 Plunker 的示例
这篇关于在表达式绑定中使用自定义全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!