获取注册表中所有实例化控件的列表 [英] Get list of all instantiated controls in registry
问题描述
我知道 OpenUI5 有一个实例化控件的注册表,可以使用
如果应用程序在低于 1.67 的 UI5 中运行,请继续阅读以了解解决方法..
≤ UI5 1.66(原答案)
<块引用>有没有办法在控制注册表中获取完整的实例列表?
有点作弊,是的!
选项 1 - 访问真正的核心
getRegisteredElements: function() {让核心;const 假插件 = {启动插件:realCore =>核心 = 真实核心};//核心"需要从sap/ui/core/Core"Core.registerPlugin(fakePlugin);Core.unregisterPlugin(fakePlugin);返回 core.mElements;},
API
选项 2 - 仅获取控件
getRegisteredControls: function() {//核心";需要从sap/ui/core/Core"返回 Core.byFieldGroupId("" | []);//传递一个空字符串或一个空数组!},
这将返回 sap.ui.core.Control
类型的所有已注册元素的数组 (来源).传递 ""
或 []
可确保返回所有控件,不管控件是否具有字段组 ID.>
选项 3 - Opa 插件
在编写测试时,另一种选择是使用专用的公共 API getAllControls
来自 sap.ui.test.OpaPlugin
:
new OpaPlugin().getAllControls();//OpaPlugin"从sap/ui/test/OpaPlugin"需要;
虽然名字暗示它会返回Controls,但它实际上也会返回Element实例.
该插件还提供了一些其他有趣的 API,例如 getMatchingControls
(带有提供 controlType?
、visible?
、interactable?
等的选项..) 这可能有用.
I understand that OpenUI5's has a registry of instantiated controls and can be queried with sap.ui.getCore().byId
.
But, is there a way to get a full list of instances in the control registry?
Something like this:
var aControls = sap.ui.getCore().allControls();
≥ UI5 1.67
With commit:54df6ca
, no more workarounds are needed. Instead, the module Element
as well as Component
provides public APIs such as .all()
, .filter()
, .forEach()
, .size
, and more. See:
API reference:
sap.ui.core.Element.registry.*
Registry of all
sap.ui.core.Elements
that currently exist.API reference:
sap.ui.core.Component.registry.*
Registry of all
Components
that currently exist.
Sample
sap.ui.require([
"sap/ui/core/Element"
], Element => console.log(Element.registry.all()));
If the application runs in UI5 below 1.67, keep reading for workarounds..
≤ UI5 1.66 (Original answer)
Is there a way to get a full list of instances in the control registry?
With a bit of cheating, yes!
Option 1 - Accessing the real core
getRegisteredElements: function() {
let core;
const fakePlugin = {
startPlugin: realCore => core = realCore
};
// "Core" required from "sap/ui/core/Core"
Core.registerPlugin(fakePlugin);
Core.unregisterPlugin(fakePlugin);
return core.mElements;
},
The API registerPlugin
awaits an object that contains a method startPlugin
(and stopPlugin
) as an argument. It invokes the startPlugin
method immediately as long as the core is initialized. As a parameter, we're getting the real core from which we can get the map of all registered elements via mElements
(thanks to the hint from Serban).
Option 2 - Getting controls only
getRegisteredControls: function() { // "Core" required from "sap/ui/core/Core"
return Core.byFieldGroupId("" | []); // pass an empty string or an empty array!
},
This will return an array of all registered elements that are of type sap.ui.core.Control
(source). Passing ""
or []
ensures that all controls are returned, regardless whether the control has a field group ID or not.
Option 3 - Opa Plugin
When writing tests, another option is to use the dedicated public API getAllControls
from sap.ui.test.OpaPlugin
:
new OpaPlugin().getAllControls(); // "OpaPlugin" required from "sap/ui/test/OpaPlugin"
Although the name suggests it will return Controls, it actually returns Element instances as well.
The plugin provides some other interesting APIs too, such as getMatchingControls
(with options to provide controlType?
, visible?
, interactable?
, etc..) which might be useful.
这篇关于获取注册表中所有实例化控件的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!