Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

sapui5 - Get list of all instantiated controls in registry

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

≥ UI5 1.67

With the commit 54df6ca, no more workarounds are needed. The modules sap/ui/core/Element and sap/ui/core/Component provide public APIs such as .all(), .filter(), .forEach(), .get(), and .size. Take a look at the API reference respectively:

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" // or "sap/ui/core/Component"
], ({ registry }) => {
  const registeredStuff = registry.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 (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 - 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 (thanks to the hint from Serban).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...