on ‎2013 Dec 03 5:52 PM
Hi
How do we create a JSON Model for the data which is defined in a .json file in the project resources
e.g. my data is stored in data.json file which is under models folder.
Now I need to create a model for it, I tried
var json = new sap.ui.model.json.JSONModel("/models/data.json");
json.setData({modelData:"sampleData"});
json.attachRequestCompleted(oData, function(){
sap.ui.getCore().setModel("json",json);
var nLength = json.getData().d.results.length;
alert(nLength);
});
But it didn't work. The data.json has the format
var sampleData = [
{<somedata>},
{<somedata>}
];
Any pointers??
Thanks
Sandip
Request clarification before answering.
Hi Sandip,
JSON format could be validated with JSONLint - The JSON Validator.
Calling method setData is not required.
Below example might help
http://plnkr.co/edit/JMfSKGrA1xUimj9kzamO?p=preview
Thanks and Regards, Venkatesh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Simon, Venkat
I tried both the approaches, but still get a problem - 4040 Not found
GET http://localhost:8080/<projectName>/models/data.json 404 (Not Found)
So the problem is somewhere else. I tried loading the resources but no help
sap.ui.localResources("<projectName>/models");
BTW, the json defined in the file had a minor issue. that is fxed - thanks venkat
Regards
Sandip
HI Venkat
Still cant get it to work. I have the view defined in a separate folder.
so i tried
var json1 = new sap.ui.model.json.JSONModel("./models/data.json");
<note the period before /model> but error
GET http://localhost:8080/<projectName>/models/data.json 404 (Not Found)
Thanks
Sandip
| 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.