Call Manager Function from XML Binding Expression

Using OpenUI5 / SAPUI5, in the XML Binding Expressions documentation, we have the ability to execute a function from a view.

new sap.m.CheckBox({ selected: "{= checkSelectedItems(${odata>CustomerId}) }" }) 

In my view controller:

 checkSelectedItems: function(sCustomerId) { return true; } 

In my opinion, I get a general error, as if she could not find my function:

 Uncaught TypeError: Cannot read property 'apply' of undefined 

I tried calling the function in several ways:

 {= .checkSelectedItems() } {= my.namespace.checkSelectedItems() } 

I even tried adding a function to the script tag on my index page to see if it has access only to global functions, but I also could not call it. Suggestions? Am I misinterpreting the documentation?

See JS Bin here: http://jsbin.com/sosotacihi/edit?html,output . I commented on CheckBox which has a problem, but if you put it you will see an error.

+6
source share
3 answers

You need to use formatter to call controller methods from the XML view.

  new sap.m.CheckBox({ selected: "{parts:['odata>CustomerId'], formatter:'.checkSelectedItems'}" }); 

This can be applied to any event trigger attribute. A common way to mention this:

 {parts:['<parameter1>', '<parameter2>', ...], formatter:'.<methodInController>'} 
+2
source

UI5 Suggests using expression binding instead of formatting functions. Expression binding is mainly for XML views, not for JS views.

0
source

To reuse a controller function in an expression binding, complex binding syntax also works:

 selected="{= ${parts: [{path: 'myModel>property'}], formatter: '.myMethodInController'} === 'foo'}" 

Currently, it only works when parts:[{path: ...}] enabled. But, of course, just because it works does not mean that we should use it. As you can see, such an expression binding can become quickly unreadable.
UI5 suggests sticking with the formatting function if binding the expression becomes difficult to read.

We recommend that you use formatting functions instead of very complex and difficult to read expressions.

See the documentation .


The syntax someFn(...) in expression binding only works if someFn is one of the global characters, for example, Math.max(...) or isNaN(...) .

0
source

All Articles