Reputation: 5054
I would like to debug in the developer console of Chrome. And there I would like to grab a component by ID.
sap.ui.getCore().byId('targetId')
or
myAppName.Component.getMetadata().
Which should return the component and not only the DOM!
And the second I am looking for is how to grab a model. I attached a model to the Component.js:
// set device model
var oDeviceModel = new JSONModel(Device);
oDeviceModel.setDefaultBindingMode("OneWay");
this.setModel(oDeviceModel, "device");
How can I grab this from the console.
myAppName.Component.getMetadata().getModels()
this returns some names of models, but only those defined inside the manifest, without data and not the one I described above.
Any idea how to get these data?
Upvotes: 1
Views: 2877
Reputation: 5542
You can also use
var oComponent = sap.ui.component("componentID");
to get a component.
The componentID
is the id you used when creating the component:
var oComponent = sap.ui.component({
name: "my.Component",
url: "my/component/location",
id: "componentID"
});
Upvotes: 1
Reputation: 2412
You can get any SAPUI5 Control by id using the byId
method:
var oControl = sap.ui.getCore().byId("ID");
The supplied id has to be the full id of the Control (Components, Views, and composite Controls usually add a prefix to their content). That means the id you can see on the top level DOM element of this Control.
As Components are not Controls, you will not find them in the DOM directly and cannot find them with the byId
method.
You can however use that to find the ComponentContainer and then use getComponentInstance
to get the actual Component.
var oComponent = sap.ui.getCore().byId("ComponentContainerId").getComponentInstance();
As for the Model, Controls and Components (technically everything inheriting from ManagedObject) has a getModel
method which takes the model name as parameter.
After setting the Model to your Component, the Component and all Controls that are content of this Component, should be able to get the Model using its name.
// Set the Model in the Component
oComponent.setModel(oModel, "myModel");
// Get the Model from the Component
sap.ui.getCore().byId("ComponentContainerId").getComponentInstance().getModel("myModel");
Upvotes: 1