on 10-18-2015 5:04 PM
Hi,
I got the following problem:
I've done the SAPUI5 walkthrough tutorial in which in Component.js a model has been set via following code where this is sap.ui.core.UIComponent
var oInvoiceModel = new JSONModel(jQuery.sap.getModulePath(sNamespace, oConfig.invoiceLocal));
this.setModel(oInvoiceModel, "invoice");
But in view controller I have no access. I have tested some ways like:
this.getView().getModel("invoice")
sap.ui.core.UIComponent.getModel("invoice")
...
But everytime the result is undefined or empty.
I thought when something is set in Component.js it is aviable everywhere...
What I want to do in the end is setting a model in Component.js and loop over some data in this model in the controller of the view to build a dynamic UI.
Any ideas how I can accomplish this?
Thank you,
Dominik
Hi Dominik,
Try like this in your controller and check?
var oComponent = this.getOwnerComponent(); //Returns the Component
oComponent.getModel("invoice");
Regards,
Sai Vellanki.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Dominik,
I guess the application is behaving like this -
1. Since, the router is defined before the model creation, first it will hit the controller onInit function.
2. Then it hits the component.js where you're creating model and setting the model at component level.
3. At onInit lifecycle the component model will not be available.
I would recommend that, in order to read you're model, you can try the below code at onAfterRendering lifecycle of controller.
onAfterRendering: function(){
var oComponent = this.getOwnerComponent();
var oModel = oComponent.getModel("invoice");
alert(JSON.stringify(oModel));
}
Regards,
Sai Vellanki.
Ok, I will test some different moments getting the model.
Another weird thing I noticed:
Getting the model in my main views controller works (and it holds my models data as seen in the log!) but saying model.getData returns an empty object in log .... oO
I don't get it...
Please see my screenshots for more info.
oData is in my model as seen in the log.
But getting the odata is not possible...
Dominik,
How are you setting the model? Consider, I am setting the model like this -
var oDataObject = [
{Product: "Power Projector 4713", Weight: "1467"},
{Product: "Gladiator MX", Weight: "321"},
{Product: "Hurricane GX", Weight: "588"},
{Product: "Webcam", Weight: "700"},
{Product: "Monitor Locking Cable", Weight: "40"},
{Product: "Laptop Case", Weight: "1289"}];
var oComponentModel = new JSONModel(oDataObject);
this.setModel(oComponentModel,"view");
Then I am able to call method .getData(), check the screenshot -
Regards,
Sai Vellanki.
Dominik,
I guess your model is like this, Stories as the parent.
var oStoryModel = {Stories:[
{},
{},
{},
{}]};
While setting the model, try like this and check you will be able to read oModel.getData()
var oStoryModel = new JSONModel(jQuery.sap.getModulePath(sNamespace, oConfig.storiesLocal));
this.setModel(oStoryModel.oData.Stories, "stories");
(or) having the same existing model, you can try like this -
oModel.getData().Stories
Regards,
Sai Vellanki.
Hi Dominik,
How about using setting and getting the model from the core something like this:
sap.ui.getCore().setModel(oModel)
and
sap.ui.getCore().getModel(oModel)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
98 | |
11 | |
11 | |
10 | |
10 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.