Search code examples
javascriptsapui5

SAPUI5 Assigning the model at the right time


I've got a function, which should read an entity from my odata-service and give it as a model to my preprocessor. When i try to debug my code, I see that the oDataModel is loaded after my preprocessor. Therefore at the preprocessor, my variable "oDataModel" is undefined.

I can't find the right order to assign my JSONModel to the variables

sap.ui.define([
"sap/ui/core/UIComponent",
"sap/ui/Device",
"Test_Start/model/models",
"sap/ui/model/odata/v2/ODataModel",
"sap/ui/core/util/XMLPreprocessor"
], function(UIComponent, Device, models, ODataModel, XMLPreprocessor) {
"use strict";
var oDataModel;
return UIComponent.extend("Test_Start.Component", {

    metadata: {
        manifest: "json"
    },


    createContent: function() {

        var oModel = new ODataModel("/XXX/"),
        oMetaModel = oModel.getMetaModel(),
        sPath = "/ColumnSet",
        oViewContainer = new sap.m.VBox();


        oMetaModel.loaded().then(function() {

            oModel.read("/DataSet(0)",{
            method: "GET",
            success:function(oData) {
                    oDataModel = new sap.ui.model.json.JSONModel(oData);
                    console.log(oDataModel);
                    },
            error: function(){

            }
            });
            var oTemplateView = sap.ui.view({
                preprocessors: {
                    xml: {
                        bindingContexts : {
                            meta : oMetaModel.getMetaContext(sPath)
                        },
                        models: {
                            meta: oDataModel
                        }
                    }
                },
                type : sap.ui.core.mvc.ViewType.XML,
                viewName: "Test_Start.view.View"
            });
            oTemplateView.setModel(oModel);
            oTemplateView.bindElement(sPath);
            oViewContainer.addItem(oTemplateView);

        });

        return oViewContainer;

    }



});


});

Solution

  • Thanks to the comment from DanielAlmeida I found the solution:

    createContent: function() {
      var oModel = new ODataModel("/sap/opu/odata/SAP/ZPFO_CKPT_ODATA_DYN_SRV/"),
        oMetaModel = oModel.getMetaModel(),
        sPath = "/ColumnSet",
        oDataModel,
        oViewContainer = new sap.m.VBox();
        oMetaModel.loaded().then(function() {
          oModel.read("/DataSet(0)",{
            method: "GET",
            success: function(oData) {
              oDataModel = new sap.ui.model.json.JSONModel(oData);
              console.log(oData);
              console.log(oDataModel);
              var oTemplateView = sap.ui.view({
                preprocessors: {
                  xml: {
                    bindingContexts: {
                      meta: oMetaModel.getMetaContext(sPath)
                    },
                    models: {
                      meta: oDataModel
                    }
                  }
                },
                type : sap.ui.core.mvc.ViewType.XML,
                viewName: "Test_Start.view.View"
              });
              oTemplateView.setModel(oModel);
              oTemplateView.bindElement(sPath);
              oViewContainer.addItem(oTemplateView);
            },
            error: function() {}
          });
        });
        return oViewContainer;
      }
    });