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
  • 17,103
  • 12
  • 72
  • 170
w0wka91
  • 189
  • 2
  • 9
  • 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

2 Answers2

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
  • 5,127
  • 2
  • 17
  • 34
  • 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

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:

<Table items="{datasources>/}" />
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
w0wka91
  • 189
  • 2
  • 9
  • 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