Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of all instantiated controls in registry

Tags:

sapui5

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();
like image 756
Jonathan.Brink Avatar asked Oct 26 '25 14:10

Jonathan.Brink


2 Answers

With the commits 54df6ca and 95445e2 included since UI5 1.67 and 1.120 respectively, no more workarounds are needed. The new static registry layer of Element and Component includes APIs such as .all(), .filter(), .forEach(), .get(), and .size. Take a look at each API reference:

sap/ui/core/ElementRegistry (1.120) / Element.registry (1.67)

Registry of all sap.ui.core.Elements that currently exist.

sap/ui/core/ComponentRegistry (1.120) / Component.registry (1.67)

Registry of all Components that currently exist.

Sample

sap.ui.require([
  "sap/ui/core/ElementRegistry", // since 1.120
], (ElementRegistry) => {
  const registeredStuff = ElementRegistry.all(); // or .filter(fn, this), .forEach(fn, this), .get("..."), .size
  console.log(registeredStuff);
});

ui5 get registered elements


If the application runs in UI5 below 1.67, keep reading for workarounds..


≤ UI5 1.66

Is there a way to get a full list of instances in the control registry?

With a bit of cheating, yes!

Option 1 - via .mElements from 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

UI5 - get registered elements

⚠️ .mElements is a private member. It has been removed in future UI5 releases!
⚠️ Core.registerPlugin() and .unregisterPlugin() are deprecated since 1.73.

Option 2 - via Core.byFieldGroupId (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 - via Opa Plugin (for tests)

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.

like image 154
Boghyon Hoffmann Avatar answered Oct 29 '25 16:10

Boghyon Hoffmann


There is no currently documented way of obtaining the complete list of elements. The elements are registered in the mElements private map (object) inside the core instance. You can check this object's usages inside the Core source code. It is never exposed directly to the outside world via a method.

Normally, you would be able to simply go around the 'private' access level which is just a convention in JavaScript and just do sap.ui.getCore().mElements, but this will not work in this case. This is because the core is wrapped into an Interface (through a closure) which only holds proxies to the public methods. This implies that you have no way of getting the real core instance from the sap.ui.getCore() call, so you cannot access the mElements property from there.

I don't know any way of obtaining the 'raw' core instance (and normally it should not be possible - the guys at SAP intended to do defensive programming here and not allow users to meddle with the core internals). If you would manage to obtain it through some way, then you could access this 'private' property and obtain the element list (actually, a map between ID and reference).

like image 42
Serban Petrescu Avatar answered Oct 29 '25 18:10

Serban Petrescu