SAPUI5在合适的时间分配模型

时间:2018-05-03 07:52:14

标签: javascript sapui5

我有一个函数,它应该从我的odata-service中读取一个实体并将其作为模型提供给我的预处理器。 当我尝试调试我的代码时,我看到oDataModel是在我的预处理器之后加载的。因此在预处理器中,我的变量" oDataModel"未定义。

我无法找到将JSONModel分配给变量的正确顺序

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;

    }



});


});

1 个答案:

答案 0 :(得分:0)

感谢comment from DanielAlmeida我找到了解决方案:

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;
  }
});