从 XML 绑定表达式调用控制器函数 [英] Call controller function from XML binding expression
问题描述
使用 OpenUI5/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?
请在此处查看 JS Bin:http://jsbin.com/sosotacihi/edit?html,输出.我已经注释掉了有问题的 CheckBox,但如果你把它放进去,你会看到错误.
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.
推荐答案
您需要使用 formatter 从 XML 视图调用控制器的方法.
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>'}
这篇关于从 XML 绑定表达式调用控制器函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!