cancel
Showing results for 
Search instead for 
Did you mean: 

Create dinamically component container

former_member187007
Active Participant
0 Kudos

Hello SCN friends,

Please I need your help with something, I have used ComponentContainer to open an app inside a another app defining statically in the XML View. So now I need to do this but in the handle of route matched of the parent app in the controller of the parent view. When I do it, this issues an error saying that the "The name of the component is undefined.", but if I put this code in the onInit method works as expected.

What could be wrong here?.

XML Code (works well)

<core:ComponentContainer id="containerForChildComponent" autoPrefixId="true" settings="{id: 'childComponent'}" height="100%" manifest="true" async="true" lifecycle="Container" usage="reuseAnotherComponent"/>

Controller of the parent app, method handleRouteMatched.

this.byId("pageAppCont").addContent( new sap.ui.core.ComponentContainer({
				 	id : "containerForChildComponent",
					autoPrefixId : true,
				  	settings : { id: "childComponent"},
					height : "100%",
				 	manifest : true,
				  	async : true,
				  	lifecycle : "Container",
				  	//name : "confCortes",
				  	usage : "reuseAnotherComponent"
				 }));

the pageAppCont is the Page object of the view of the parent app.

This the error:

I need to do it programatically because I'm using a menu where I need to be destroying the component container and creating again with the corresponding app component (I'm only using a view for hosting every app, one at time), ComponentContainer doesn't allow to set the usage property more than once. The following are the properties of the parent app in the manifest.json file regarding with component usage.

Thank you very much.

Jhon Jairo.

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor
0 Kudos

Hi Jhon Jairo Teran Salazar,

I tried it, didnt work for me as well. It seems if we are creating the componentContainer in handleRouteMatched the ownerComponent is not set for the child component, which is the cause of the issue.

It seems that it is a bug with UI5? dont know for sure.

But you can do a workaround for this.

In the Init method, create the component container -> this will not affect as the component is already loaded by the framework, we are just create the container. Pass it to this eg.,

//onInit
this.comp = new sap.ui.core.ComponentContainer({
				usage: "myreuse"
			});
// Routematched
this.getView().byId("page").addContent(this.comp );

BR,

Mahesh

former_member187007
Active Participant

Helo Mahesh,

You are right, I found yesterday this issue reported in openui5 on github, that says the workaround for this problem is to call this.getOwnerComponent().runAsOwner and in the callback of this method call the addContent.

Thank you.

Jhon Jairo

maheshpalavalli
Active Contributor

Thanks Jhon Jairo Teran Salazar for sharing this info, I was not knowing that there is a method call runAsOwner component.

Mahesh

maheshpalavalli
Active Contributor

Actually when i debugged yesterday, I found that the managedObject is not having the ownerId when called from the routematched and it has when called from the onInit method.

Now I went to the runAsOwner documentation and was able to understand the behavior. Becuase when we create the componet container, it creates the managed object and which will not have ownerId as the Documentation below says that they are set only at the time of craetion(at least when the framework creates them like views). So if we call this method, it sets the ownerId to any managedobject that gets created in its handler function.

Returns the ID of the object in whose "context" the given ManagedObject has been created.
For objects that are not ManagedObjects or for which the owner is unknown, undefined will be returned as owner ID.
Note: Ownership for objects is only checked by the framework at the time when they are created. It is not checked or updated afterwards. And it can only be detected while the Component.runAsOwner function is executing. Without further action, this is only the case while the content of a UIComponent is constructed or when a Router creates a new View and its content.

Answers (3)

Answers (3)

former_member187007
Active Participant
0 Kudos

Thank you very much Mahesh for complementing this, now it's very clear what was happening.

Best regards.

Jhon Jairo

SergioG_TX
Active Contributor
0 Kudos

instead of destroying and recreating, you may be able to update the component object w a data driven model?? it would be a better approach

former_member187007
Active Participant
0 Kudos

Hello Sergio, thanks for your answer, actually I tried to do that but I can only set the property once, that's the problem, it doesn't matter how many times I change the "usage" property, always it's going to be with the first component you attach :(.

Thanks.

Jhon Jairo.

former_member187007
Active Participant
0 Kudos

This what it says the documentation:

SergioG_TX
Active Contributor
0 Kudos

hi Jhon...

can you try to see if the component is available in the onAfterRendering function and not on the onInit?

i believe the error u r seeing is due to the loading/timing of the child component. also, you may be able to use a different approach than destroying the object