0

i want to bind a global Model and access this in different xml views but it always shows no content.

In the Controller i bind the data this way...

sap.ui.define([
    'jquery.sap.global',
    'sap/ui/core/mvc/Controller',
    'sap/ui/model/json/JSONModel'
], function (jQuery, Controller, JSONModel) {
    "use strict";

    return Controller.extend("sap.ui.demo.controller.DatasourceManagement", {
        onInit: function() {
            var datasourcesModel = new JSONModel('/api/datasources');
            sap.ui.getCore().setModel(datasourcesModel, 'datasources');
            //this.getView().setModel(datasourcesModel,'datasources');
        },
....

And in the View i try to access the data this way...

mvc:View xmlns="sap.m" xmlns:tnt="sap.tnt" xmlns:l="sap.ui.layout"
    xmlns:mvc="sap.ui.core.mvc"
    controllerName="sap.ui.demo.controller.DatasourceManagement">

    <Page showHeader="false" enableScrolling="true" class="sapUiContentPadding test"
        showNavButton="false">

        <content>
            <Table id="datasourceTable"
                items="{path: 'datasources>',
                        sorter: {
                        path: 'id'
                       }}"
....

What is the problem with my code?

-------EDIT 1------------- The URL "/api/datasources" returns an array of entries:

[
  {
    "id": 1,
    "name": "FTPSERVER",
    "hostname": "test.de",
    "port": 21,
    "username": "username",
    "password": "password"
  },
  {
    "id": 2,
    "name": "FTPSERVERasdasdasdadsads1111111",
    "hostname": "test.de",
    "port": 21,
    "username": "username",
    "password": "password"
  },
  {
    "id": 3,
    "name": "FTPSERVER",
    "hostname": "test.de",
    "port": 21,
    "username": "username",
    "password": "password"
  }
]
Boghyon Hoffmann
  • 15,517
  • 8
  • 61
  • 146
w0wka91
  • 139
  • 2
  • 8
  • 1
    Does this answer your question? [Global Model Not Accesible](https://stackoverflow.com/questions/33121909/global-model-not-accesible) – Boghyon Hoffmann Jul 17 '21 at 14:32

3 Answers3

1

TL DR

I think it is because of your Table items binding path, specifically you should change it to 'datasources>/' instead of 'datasources>'.

Reasoning

Based on the code snippet that you have shown, I assume that "/api/datasources" returns an array/map of entries.

Working with the assumption that the HTTP request gives you back a non-empty array (which you should check in the Developer Console --> Network tab), I see only one problem in your code: the binding path for the Table's items is relative.

In UI5, bindings are of two types:

  • Relative: the path does not start with "/" (e.g. path: 'datasources>')
  • Absolute: the path starts with "/" (e.g. path: 'datasources>/')

Absolute bindings are resolved directly, by taking the object at the given path in the model. Relative bindings on the other hand are resolved relatively to some path specified by an ancestor. If there is no other ancestor element binded (absolutely) to something of this JSON Model, then the binding will not be resolved.

Serban Petrescu
  • 4,987
  • 2
  • 16
  • 33
  • I tried the proposed binding 'datasources>/' but it does not work. When i bind the model directly to the view with 'this.getView().setModel(datasourceModel,'datasources')' it does work as expected but i need to use the global model. – w0wka91 Feb 25 '17 at 15:36
  • Hmm, the fact that it works with the view-local model is strange (because of the binding rules). Do you happen to have a model with the same name defined on an upper level (e.g. containing view or component)? Because that is the only way that I can think of that the global model propagation would not work. – Serban Petrescu Feb 25 '17 at 16:19
0

The main problem is that the model is not automatically known to the children of your component if that model is set to the core. You can change this behavior though. Here is an answer to someone who had a similar problem: https://stackoverflow.com/a/42251431/5846045

Also as Serban pointed out, the binding path syntax is missing a / at the end to access the array in the absolute path. So it should be: datasources>/ .

Boghyon Hoffmann
  • 15,517
  • 8
  • 61
  • 146
0

Now i got the solution...

when i bind the model to the component it works:

this.getOwnerComponent().setModel(datasourcesModel, 'datasources');

and in the view i can bind the model that way:

<core:View xmlns="sap.m" xmlns:tnt="sap.tnt" xmlns:l="sap.ui.layout"
    xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
    controllerName="sap.ui.demo.controller.DatasourceManagement">

    <Page showHeader="false" enableScrolling="true" class="sapUiContentPadding test"
        showNavButton="false">

        <content>
            <Table id="datasourceTable"
                items="{path: 'datasources>/',
                        sorter: {
                        path: 'id'
                       }}"

i am very grateful for your help...

w0wka91
  • 139
  • 2
  • 8
  • 1
    A better way would be to define your model in the manifest.json. This will make it accessible globally. – Stephen S Feb 26 '17 at 14:37
  • 1
    You can create a model as `"datasources": { "type": "sap.ui.model.json.JSONModel", "settings": {}, "preload": false }`. You can refere the guide [here](https://sapui5.netweaver.ondemand.com/#docs/guide/44062441f3bd4c67a4f665ae362d1109.html) for Odata model & replace it with the above code – Stephen S Feb 27 '17 at 09:43