Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call controller function from XML binding expression

Tags:

sapui5

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

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

In my controller behind the view:

checkSelectedItems: function(sCustomerId) {
    return true;
}

In my view, I get the generic error as if it cannot find my function:

Uncaught TypeError: Cannot read property 'apply' of undefined

I've tried calling the function in several ways:

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

I even tried adding a function in a script tag in my index page to see if it only has access to global functions, but I wasn't able to trigger that either. Suggestions? Am I misinterpreting the documentation?

Please see the JS Bin here : http://jsbin.com/sosotacihi/edit?html,output. I've commented out the CheckBox that has the issue, but if you put it in, you'll see the error.

like image 809
mitch Avatar asked Jul 24 '15 16:07

mitch


1 Answers

You need to use formatter to call methods of controller from an XML View.

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

This can be applied to any event triggering attribute. The generic way to mentioned this is:

{parts:['<parameter1>', '<parameter2>', ...], formatter:'.<methodInController>'}
like image 61
Vijayendra Gade Avatar answered Oct 03 '22 04:10

Vijayendra Gade