I’ve just jumped in to OpenUI5, and had a problem where I couldn’t get navigation targets to open up in aggregations within sub views. I found an answer on Stack Overflow that said that I needed to specifically name each View in the parent hierarchy to stabilise the name – this mostly worked, and I’ve been able to get my routing and targets to function.
My problem now is that the ID prefix of my item is __component0
. I cannot find any way to change the name of this part, so really I’m not fully in control of my IDs.
I’ve tried changing the sId
of the component before and after initialisation, but then the app doesn’t function. I’ve also set both sap.app.componentName and sap.ui5.componentId, and neither are used. It appears that component0
is constructed by getting the type name of the controller class, but that has to be called Component.js
.
Does anybody know how to change this value, or otherwise derive a control ID in the DOM that is fully defined in code?
In response to boghyon, I don’t doubt that setting the ID via the factory function works, I don’t know how to apply that ID value or factory function to the style of code that I have. This style comes from the SAP starter template on GitHub. Simply specifying id: "mycomponentname"
in the extend object does not work.
So how would I go about refactoring what I have to the factory function format?
sap.ui.define([
"sap/ui/core/UIComponent",
"sap/ui/Device",
"com/uk/ia/air/cpanel/model/models"
], function(UIComponent, Device, models) {
"use strict";
return UIComponent.extend("com.uk.ia.air.cpanel.Component", {
metadata: {
manifest: "json"
},
/**
* The component is initialized by UI5 automatically during the startup of the app and calls the init method once.
* @public
* @override
*/
init: function() {
// call the base component's init function
UIComponent.prototype.init.apply(this, arguments);
// set the device model
this.setModel(models.createDeviceModel(), "device");
// create the views based on the url/hash
this.getRouter().initialize();
}
});
});
2
Answers
The ID of a component can be manually assigned when instantiating the component
sap/ui/core/Component#create
APInew ComponentContainer(/*...*/)
APIWith
ComponentContainer
E.g. in
index.html
when bootstrapping:The module, that can create a
ComponentContainer
inindex.html
, issap/ui/core/ComponentSupport
. Once it’s loaded, it will look for the HTML element that has the attributedata-sap-ui-component
. That HTML element should provide constructor settings for theComponentContainer
which providessettings
for the component constructor same as inComponent.create
.With
Component.create()
For more information, see Stable IDs: All You Need to Know.
Additionally, every view must have an ID as well so that the given component ID is included in the full ID of an element. Otherwise, the component ID won’t be available even if we defined it in the component factory function. View IDs can be set via the property
id
andviewId
respectively in the application descriptor (manifest.json):sap.ui5 / rootView /
id
sap.ui5 / routing / targets /
targetName
/
viewId
You can change the autogenerated ID of the extended component if you override the constructor of the UIComponent. With your code it should look like this: