'Function Call in Expression Binding in OpenUI5/SAPUI5

Problem is to print out the current year by using expression binding that was introduced with 1.28.

Expression Binding is capable of executing global accessible JS Functions.

Goal: Execute (new Date()).getFullYear() in expression binding

  • In this JSFiddle the first attempt is to execute a global string {:=Math.PI} => works
  • Second attempt is to define a global function window.temp = function() {return 'test'}; and to execute it via <Text text="{:=temp()}" /> => fail
  • Goal <Input value="{:=(new Date()).getFullYear()}" /> breaks parser and is in jsfiddle therefor outcommented. The Error Message is Expected ) but instead saw Date at position 9 - {:=(new Date()).getFullYear()} sap.ui.base.ExpressionParser

First question is: Why does the parser break? Is it a bug or am I doing something wrong?

Second question is: Even if the goal is not possible. Why is my second attempt also not working?



Solution 1:[1]

I assume that the parser breaks as it does not support the new operator. Your second example is not working as only functions which are available via global symbols can be used. The window object is not listed there.

Solution 2:[2]

Had a similar problem. As commenters have stated, function calls from global symbols are allowed, so an obvious hack would be:

On init:

Date.evilhack = (a => new Date(a));

And in the expression binding:

{= Date.evilhack(${something}).getFullYear() }

If anyone has a better idea, please share! I know there are custom formatters, but I'm looking for a minimal, hopefully clean, solution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 matbtt
Solution 2 Justin Sane