cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

getComponent

Former Member
0 Likes
6,540

Hi,

I'm building an App using the Component approach which is great. But now I'm facing the problem that I can't access my parent (or any other) component from within a component. I came to know that there is a method getComponent on the core but


sap.ui.getCore().getComponent("myComponentId")

does not give me anything.


sap.ui.getCore().getRootComponent()

doesn't return anything either.

This is the code I use to create the component:


new sap.ui.core.ComponentContainer("componentId", {

        height : "100%",

        width : "100%",

        name : "com.component.my.nice",

        propagateModel: true

      });

The whole thing is running in a sap.m.Shell + sap.m.SplitApp. The SplitApp is created as the root component of the Shell in my index.html like this:


new sap.m.Shell("Shell", {

            app: new sap.ui.core.ComponentContainer("RootContainer", {

              height : "100%",

              width : "100%",

              name : "com.component.root",

              displayBlock : "true",

              mode : "HideMode"

            })

          }).placeAt("content");


What am I doing wrong and why can't I query my components? How can I access the general component registry?


Cheers

Christian

View Entire Topic
Former Member

To avoid this problem I added this method to all of my controllers:


    getComponent: function () {

        var sComponentId = sap.ui.core.Component.getOwnerIdFor(this.getView());

        return sap.ui.component(sComponentId);

    },

Former Member
0 Likes

Searched some time for this snippet - brilliant! Thank you Judith!

alexander_gahr
Employee
Employee

You normally dont need to put it inside the controller. You normally should be able to use :

this.getOwnerComponent() to the component.

Alex