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

getComponent

Former Member
0 Likes
6,535

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

Accepted Solutions (1)

Accepted Solutions (1)

Hi Christian,

in order to provide an ID for a component, you have to set it at component instantiation, as e.g. in the following code snippet

   var oComp = sap.ui.getCore().createComponent({    

       name: "my.component.class",

       id: "myComp",

       height: "100%"

   });

  var oComponentContainer = new sap.ui.core.ComponentContainer({

         component : oComp

  });

  oComponentContainer.placeAt("content");

then you can access the component later on via

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

Former Member
0 Likes

Thx a lot Leonhard. It works for getComponent now. getRootComponent still returns undefined. Any idea how to get that to work?

yunustuzun
Participant
0 Likes

I guess , you should declare it in manifest.json file for geting root component . Like this :

"componentUsages":{"reuse":{"name":"sap.reuse.component","lazy":false}}

Answers (3)

Answers (3)

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

TheVivekGowda
Explorer
0 Likes

I use below code snippet to access root component

sap.ui.core.Component.getOwnerComponentFor(this.getView())

Using this we can access root component models & router too. I do create helper functions in child app base controller file so that I can reuse it throughout the app.

/**
* Returns root component instance
*/ 
getRootComponent: function(){
  return sap.ui.core.Component.getOwnerComponentFor(this.getView());
},
Former Member
0 Likes

Hi Christian

Did you try sap.ui.getCore().byId('componentId')?

-D