Search code examples
sapui5

Call controller function from XML binding expression


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.


Solution

  • 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>'}