Reputation: 328
Framework : SAPUI5 V.1.38.39
IDE : WebIde
I created a JSON file Data.json in the model folder and i would like to use in in the onInit of my controller to create a JSONmodel, my actual problem is that it's always returning me a 404 response on Data.json .
The folder tree it the initial one from SAPUI5 application template
test 1 in webapp/controller/App.contoller.js:
onInit: function () {
var oBusy = new sap.m.BusyDialog();
var oModel = new JSONModel();
oModel.attachRequestSent(function() {
oBusy.open();
});
oModel.loadData("model/Data.json"); //here the path so I suppose here the problem
oModel.attachRequestCompleted(function() {
oBusy.close();
});
this.getView().setModel(oModel, "CompaniesModel")
}
test 2 in webapp/controller/App.contoller.js:
onInit: function () {
var oBusy = new sap.m.BusyDialog();
var oModel = new JSONModel();
oModel.attachRequestSent(function() {
oBusy.open();
});
oModel.loadData("../model/Data.json"); //here the path so I suppose here the problem
oModel.attachRequestCompleted(function() {
oBusy.close();
});
this.getView().setModel(oModel, "CompaniesModel")
}
test 3 in webapp/controller/App.contoller.js:
onInit: function () {
var oBusy = new sap.m.BusyDialog();
var oModel = new JSONModel();
oModel.attachRequestSent(function() {
oBusy.open();
});
oModel.loadData("my/namespace/model/Data.json"); //here the path so I suppose here the problem
oModel.attachRequestCompleted(function() {
oBusy.close();
});
this.getView().setModel(oModel, "CompaniesModel")
}
In Webide for SAPUI5 create new from template SAPUI5 application selection SAP version 1.38.39, initialize your namespace and your view/controller name.
In model create new Data.json :
{
"Set1": [{
"Status": "status1",
"Number": 10
}, {
"Status": "status2",
"Number": 20
}, {
"Status": "status3",
"Number": 30
}, {
"Status": "status4",
"Number": 40
}]
}
in your view :
<mvc:View
controllerName="your.namespace.controller.App"
xmlns:l="sap.ui.layout"
xmlns:core="sap.ui.core"
xmlns:mvc="sap.ui.core.mvc"
xmlns:viz="sap.viz.ui5.controls"
xmlns:viz.feeds="sap.viz.ui5.controls.common.feeds"
xmlns:viz.data="sap.viz.ui5.data"
xmlns:com="sap.suite.ui.commons"
xmlns="sap.m">
<Shell id="shell">
<App id="app">
<pages>
<Page id="page" title="{i18n>title}">
<content>
<l:VerticalLayout class="sapUiContentPadding" width="100%">
<com:ChartContainer id="chartContainer" showFullScreen="true" showPersonalization="false" autoAdjustHeight="false"
personalizationPress="attachPersonalizationPress" contentChange="attachContentChange" title="chart">
<com:content>
<com:ChartContainerContent icon="sap-icon://line-chart" title="Line Chart">
<com:content>
<viz:VizFrame id="idVizFrame_1" height='400px' width="100%" vizType='column' uiConfig="{applicationSet:'fiori'}">
<viz:dataset>
<viz.data:FlattenedDataset data="{CompaniesModel>/Set1}">
<viz.data:dimensions>
<viz.data:DimensionDefinition name="Status" value="{CompaniesModel>Status}"/>
</viz.data:dimensions>
<viz.data:measures>
<viz.data:MeasureDefinition name="Number" value="{CompaniesModel>Orders}"/>
</viz.data:measures>
</viz.data:FlattenedDataset>
</viz:dataset>
<viz:feeds>
<viz.feeds:FeedItem uid="valueAxis" type="Measure" values="Number"/>
<viz.feeds:FeedItem uid="categoryAxis" type="Dimension" values="Status"/>
</viz:feeds>
</viz:VizFrame>
</com:content>
</com:ChartContainerContent>
</com:content>
</com:ChartContainer>
</l:VerticalLayout>
</content>
</Page>
</pages>
</App>
</Shell>
</mvc:View>
And in your controller :
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel",
], function (Controller, JSONModel) {
"use strict";
return Controller.extend("your.namespace.controller.App", {
onInit: function () {
//copy here one of the controller described in Problem in code & test
}
});
});
I add this possibility here if you don't want to use manifest, for using manifest go to the accepted answer of @Cmdd :)
if you don't want to use manifest you can do :
oModel.loadData("../../../../../webapp/model/Data.json");
Upvotes: 0
Views: 10871
Reputation: 897
Best way is to load the data in the manifest.json
, as described in this link.
In "sap.app" section:
"sap.app": {
...
"dataSources": {
"myModel_alias": {
"uri": "model/Data.json",
"type": "JSON"
}
}
}
in "sap.ui5" section:
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "cmdd.Test.i18n.i18n"
}
},
"CompaniesModel": {
"type": "sap.ui.model.json.JSONModel",
"dataSource": "myModel_alias"
}
},
Also, don't forgot to put a slash in this binding: data="{CompaniesModel>/Set1}"
:)
Upvotes: 2