我了解OpenUI5具有实例化控件的注册表,可以通过来查询sap.ui.getCore().byId。
但是,有没有办法在控制注册表中获取实例的完整列表?
像这样:
var aControls = sap.ui.getCore().allControls();
Run Code Online (Sandbox Code Playgroud)
使用commit:54df6ca,无需其他解决方法。相反,模块Element以及Component提供公共API,例如.all(),.filter(),.forEach(),.size,等等。看到:
sap.ui.core.Element.registry.*
sap.ui.core.Elements当前所有存在的注册表。
sap.ui.core.Component.registry.*
Components当前所有存在的注册表。
sap.ui.require([
"sap/ui/core/Element"
], Element => console.log(Element.registry.all()));
Run Code Online (Sandbox Code Playgroud)
如果应用程序在低于1.67的UI5中运行,请继续阅读以获取解决方法。
有没有办法在控制注册表中获取实例的完整列表?
有点作弊,是的!
getRegisteredElements: function() {
let core;
const fakePlugin = {
startPlugin: realCore => core = realCore
};
sap.ui.getCore().registerPlugin(fakePlugin);
sap.ui.getCore().unregisterPlugin(fakePlugin);
return core.mElements;
},
Run Code Online (Sandbox Code Playgroud)
API registerPlugin等待包含一个方法startPlugin(和stopPlugin)作为参数的对象。只要核心已初始化,它就会立即调用该startPlugin方法。作为一个参数,我们得到了真正的核心,我们可以从中获取所有已注册元素的映射mElements(感谢Serban的提示)。
getRegisteredControls: function() {
return sap.ui.getCore().byFieldGroupId(""); // pass an empty string!
},
Run Code Online (Sandbox Code Playgroud)
这将返回类型为sap.ui.core.Control (source)的所有已注册元素的数组。空字符串("")确保返回所有控件,无论该控件是否具有字段组ID。
编写测试时,另一种选择是使用getAllControls来自的专用公共API sap.ui.test.OpaPlugin:
new OpaPlugin().getAllControls(); // OpaPlugin required from "sap/ui/test/OpaPlugin"
Run Code Online (Sandbox Code Playgroud)
尽管顾名思义,它将返回Controls,但实际上它也返回Element实例。
该插件提供了其他一些有趣的API太多,如getMatchingControls(有选项来提供controlType?,visible?,interactable?等..),这可能是有用的。
| 归档时间: |
|
| 查看次数: |
805 次 |
| 最近记录: |