0

I try to fix this for several hours but cann't see my mistake. I try to do just a simple binding:

in my index.html

data-sap-ui-xx-bindingSyntax="complex"

...

var oModel = new sap.ui.model.json.JSONModel();
oModel.loadData("model/data.json");
sap.ui.getCore().setModel(oModel);

in my App.view.xml

...
<Page title="{/greeting}">
...

data.json

{
"greeting": "hey"
}

I cannot see what's wrong here. Even a sap.ui.getCore().getModel() during debugging gives me a Object with {"greeting":"hey"} in it's oData variable.

I hope you guys can help.

I also posted here the original question. But since I simplified it, I should post this in a seperate question. Binding in List with XML

--------- update --------

this.getView().setModel(oModel); -> works

sap.ui.getCore().setModel(oModel); -> doesn't work

2 Answers 2

0

I think you meant to use an absolute binding path in your application. This means that you will have to prefix it with a slash:

<Page title="{/greeting}">

You can find more about the binding path syntax for JSON models here: http://help.sap.com/saphelp_hanaplatform/helpdata/en/d5/2e364907f94a3caeb4f5e5ad0cf302/content.htm

Another thing I noticed is that you declare oModel, but load your data into oPositoModelionsModel. I think you intended to load your data into oModel instead:

var oModel = new sap.ui.model.json.JSONModel();
oModel.loadData("model/data.json");
sap.ui.getCore().setModel(oModel);

The rest of the code seems very legit. Have a look at this jsbin in which I have modified your code slightly, so that it pulls data from the the Star Wars API.

If the Model is not inherited from core to your view, the inheritance link may be broken somewhere. This happens in e.g. dialog boxes. Dialogs are not added to the UI tree, but the addDependent method will still connect the dialog to the lifecycle management and data binding of underlying UI component (e.g. view). You can read more about this in Step 16 of the UI5 walk-through.

10
  • Sry, my bad. In my original code there is {/greeting}. Was a typo just in the question I changed it now also in my question.
    – Michael
    Jun 30, 2016 at 8:12
  • Found another possible error in your code as well as a working example based on your code. Jun 30, 2016 at 8:18
  • Again: just a Typo. In the code there is just a oModel and no oPositionsModel. Sorry for posting so many typos. Edited this too in the quesiton.
    – Michael
    Jun 30, 2016 at 8:26
  • I guess then you should just have a look at the jsbin containing your fragments of code. It's pulling data from an external service as well. Compare that to your own code, swap some code between the two and see when it breaks. When it breaks, you found the culprit. Jun 30, 2016 at 8:32
  • Please have a look on my update. I did have a look but <our JSbin is not working for me. Our Firewall blocks swapi.co.
    – Michael
    Jun 30, 2016 at 8:37
0

If you assign your model to the global namespace of the application (sap.ui.getCore().setModel()), the model should automatically bound to the views. You can bind them to the view (or the page itself) directly.

It's recommended to assign models to the views (unless if you want to store cross-view data, in this case you can assign it to the core - but it's recommended to add a name to this model.

So just move the model to the View by using this.getView().setModel() in the view's controller. (as you are trying to store the view name, it seems to be a view specific model).

11
  • thank you. But the Doc of SAP (help.sap.com/saphelp_hanaplatform/helpdata/en/91/…) says it's possible. And sap.ui.getCore().setModel(oModel, "data"); is not working either
    – Michael
    Jun 30, 2016 at 8:47
  • It's also not true. Models are inherited from their parent controls. Core -> Component -> View. The jsbins I posted in my answer prove this. It is true that it's a good practice to link your models to your component or views instead of core though. This e.g. allows you to run your app from the Fiori Launchpad without the chance of conflicts with other apps that are also using core (such as FLP itself). Jun 30, 2016 at 8:52
  • It's not a clear topic at UI5, but based on my experiences (and official UI5 applications), it's safer to use view specific models for UI related/cofniguration data. Of course, if you define a model in manifest.json, it will be a global model and accessible from every view - as desired. In addition to that, if you are using component in your project, you can assign the model to the component itself (with a name). With this, your model will be available in every view.
    – nistv4n
    Jun 30, 2016 at 8:54
  • 1
    Sorry, that's also wrong. Manifests are loaded from a certain place, which is most of the times the application's Component. This means that the models defined in the manifest are connected to that spot (e.g. Component), not to core. Hence they're not available globally, and will only be available from the views that inherit from that component. Jun 30, 2016 at 8:57
  • 1
    it's good to see, there are some issues here and it's not just my misunderstanding :D Can I ouline: 1. use Component models for View crossed stuff (defined in the Manifest or in the Component.js), 2. use View models for one View stuff and 3. don't use Core Models because some other App might use also a Core Model ?
    – Michael
    Jun 30, 2016 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.