on ‎2014 Aug 22 12:44 PM
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
Request clarification before answering.
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")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
},
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You normally dont need to put it inside the controller. You normally should be able to use :
this.getOwnerComponent() to the component.
Alex
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());
},
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Christian
Did you try sap.ui.getCore().byId('componentId')?
-D
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 9 | |
| 7 | |
| 6 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.