承诺不起作用(对我来说)

时间:2018-05-09 15:37:49

标签: sapui5

可能我在这里遗漏了一些基本的东西。场景如下:

在我的应用程序的根视图控制器(App.controller.js,onInit函数)中,我在命名模型上执行一系列读取操作,以便准备好主要供使用的其他数据由后续视图中的格式化程序。我所做的就是使用promises来确保数据准备就绪。

问题是应用程序在根视图之后加载带有模式""(在我的情况下为Worklist.view.xml)的初始视图时偶尔会崩溃(App.view.xml)因为格式化程序功能无法找到预期的数据。添加了几个断点并观察了调试器:

  1. 停在第一个阅读功能
  2. 最后停留在承诺条件
  3. 在后续视图
  4. 处停止(并抛出错误)格式化程序功能
  5. 停止oData read(resolve)的成功功能
  6. 页面由于上面引发的异常而冻结
  7. "正常"当应用程序没有崩溃时,流程如下:

    1. 停在第一个阅读功能
    2. 最后停留在承诺条件
    3. 停止oData read(resolve)的成功功能
    4. 停止readyToGo功能(它仅用于临时调试)
    5. 在后续视图中停止格式化程序功能
    6. 页面显示,一切按顺序
    7. 从上述情况来看,我得出的结论是我的承诺不起作用。有什么想法吗?

      sap.ui.define([
        "kristal/apps/agreements/controller/BaseController",
        "sap/ui/model/json/JSONModel"
      ], function (BaseController, JSONModel) {
        "use strict";
      
        /* PROMISE VARIABLES */
        var oModelTypeDataDeferred = jQuery.Deferred();
        var oModelStatusDataDeferred = jQuery.Deferred();
        var oModelRoleDataDeferred = jQuery.Deferred();
        var oModelRefDataDeferred = jQuery.Deferred();
        var oModelExtOrgDataDeferred = jQuery.Deferred();
        var oModelInvolvementDataDeferred = jQuery.Deferred();
      
        return BaseController.extend("kristal.apps.agreements.controller.App", {
          onInit: function () {
            /* CALLED AFTER PROMISES HAVE BEEN FULFILLED */
            var readyToGo = function() { 
              jQuery.sap.log.error("Ready!");
            };
            var oViewModel;
            var fnSetAppNotBusy;
            var iOriginalBusyDelay = this.getView().getBusyIndicatorDelay();
            var oViewModel = new JSONModel({
              busy: true,
              delay: 0
            });
            this.setModel(oViewModel, "appView");
            fnSetAppNotBusy = function() {
              oViewModel.setProperty("/busy", false);
              oViewModel.setProperty("/delay", iOriginalBusyDelay);
            };
            this.getOwnerComponent().getModel().metadataLoaded().then(fnSetAppNotBusy);
            var oModel = this.getOwnerComponent().getModel("f4");
            /* oData READ OPS */
            oModel.metadataLoaded().then(function(){
              // Initialize additional data
              var sPath = "/Agreement_TypesSet";
              oModel.read(sPath, {
                success: function(oData, oResponse) {
                  oModelTypeDataDeferred.resolve();
                },
                error: function(oError) {
                  jQuery.sap.log.error("Error", oError);
                }
              }); 
              sPath = "/Agreement_StatusesSet";
              oModel.read(sPath, {
                success: function(oData, oResponse) {
                  oModelStatusDataDeferred.resolve();
                },
                error: function(oError) {
                  jQuery.sap.log.error("Error", oError);
                }
              });
              sPath = "/Role_TypesSet";
              oModel.read(sPath, {
                success: function(oData, oResponse) {
                  oModelRoleDataDeferred.resolve();
                },
                error: function(oError) {
                  jQuery.sap.log.error("Error", oError);
                }
              });
              sPath = "/Reference_TypesSet";
              oModel.read(sPath, {
                success: function(oData, oResponse) {
                  oModelRefDataDeferred.resolve();
                },
                error: function(oError) {
                  jQuery.sap.log.error("Error", oError);
                }
              });
              sPath = "/External_OrganizationsSet";
              oModel.read(sPath, {
                success: function(oData, oResponse) {
                  oModelRefDataDeferred.resolve();
                },
                error: function(oError) {
                  jQuery.sap.log.error("Error", oError);
                }
              });
              sPath = "/Involvement_TypesSet";
              oModel.read(sPath, {
                success: function(oData, oResponse) {
                  oModelInvolvementDataDeferred.resolve();
                },
                error: function(oError) {
                  jQuery.sap.log.error("Error", oError);
                }
              });
              /* IF ALL PROMISES FULFILLED, PROCEED */
              jQuery.when(oModelTypeDataDeferred, oModelStatusDataDeferred, oModelRoleDataDeferred, oModelRefDataDeferred, oModelExtOrgDataDeferred, oModelInvolvementDataDeferred)
                .done().then(jQuery.proxy(readyToGo, this));
          },
          // ...
        });
      });
      

      的manifest.json:

      {
        "_version": "1.4.0",
        "sap.app": {
          "id": "kristal.apps.agreements",
          "type": "application",
          "i18n": "i18n/i18n.properties",
          "title": "{{appTitle}}",
          "description": "{{appDescription}}",
          "applicationVersion": {
            "version": "1.0.0"
          },
          "dataSources": {
            "mainService": {
              "uri": "/DEV/sap/opu/odata/SAP/ZCONTRACTS_SRV/",
              "type": "OData",
              "settings": {
                "odataVersion": "2.0",
                "localUri": "localService/metadata.xml"
              }
            }
          },
          "sourceTemplate": {
            "id": "sap.ui.ui5-template-plugin.1worklist",
            "version": "1.38.3"
          }
        },
        "sap.ui": {
          "technology": "UI5",
          "icons": {
            "icon": "sap-icon://task",
            "favIcon": "",
            "phone": "",
            "phone@2": "",
            "tablet": "",
            "tablet@2": ""
          },
          "deviceTypes": {
            "desktop": true,
            "tablet": true,
            "phone": true
          },
          "supportedThemes": [
            "sap_hcb",
            "sap_bluecrystal"
          ]
        },
        "sap.ui5": {
          "rootView": {
            "viewName": "kristal.apps.agreements.view.App",
            "type": "XML",
            "id": "app",
            "async": true
          },
          "dependencies": {
            "minUI5Version": "1.38.0",
            "libs": {
              "sap.ui.core": {
                "minVersion": "1.38.0"
              },
              "sap.m": {
                "minVersion": "1.38.0"
              },
              "sap.ushell": {
                "minVersion": "1.38.0"
              },
              "sap.collaboration": {
                "minVersion": "1.38",
                "lazy": true
              }
            }
          },
          "contentDensities": {
            "compact": true,
            "cozy": true
          },
          "models": {
            "i18n": {
              "type": "sap.ui.model.resource.ResourceModel",
              "settings": {
                "bundleName": "kristal.apps.agreements.i18n.i18n"
              }
            },
            "": {
              "dataSource": "mainService",
              "preload": true,
              "settings": {
                "defaultBindingMode": "TwoWay"
              }
            },
            "f4": {
              "dataSource": "mainService",
              "preload": true,
              "settings": {
                "metadataUrlParams": {
                  "sap-documentation": "heading"
                },
                "defaultBindingMode": "TwoWay"
              }
            }
          },
          "routing": {
            "config": {
              "routerClass": "sap.m.routing.Router",
              "viewType": "XML",
              "viewPath": "kristal.apps.agreements.view",
              "controlId": "app",
              "controlAggregation": "pages",
              "bypassed": {
                "target": [
                  "notFound"
                ]
              },
              "async": true
            },
            "routes": [{
              "pattern": "",
              "name": "worklist",
              "target": [
                "worklist"
              ]
            }, {
              "pattern": "AgreementsSet/{objectId}",
              "name": "object",
              "target": [
                "object"
              ]
            }],
            "targets": {
              "worklist": {
                "viewName": "Worklist",
                "viewId": "worklist",
                "viewLevel": 1
              },
              "object": {
                "viewName": "Object",
                "viewId": "object",
                "viewLevel": 2
              },
              "objectNotFound": {
                "viewName": "ObjectNotFound",
                "viewId": "objectNotFound"
              },
              "notFound": {
                "viewName": "NotFound",
                "viewId": "notFound"
              }
            }
          }
        },
        "sap.platform.abap": {
          "uri": "/sap/bc/ui5_ui5/sap/zctr_contr_mnt/webapp",
          "_version": "1.1.0"
        }
      }
      

      Component.js

      sap.ui.define([
        "sap/ui/core/UIComponent",
        "sap/ui/Device",
        "kristal/apps/agreements/model/models",
        "kristal/apps/agreements/controller/ErrorHandler"
      ], function(UIComponent, Device, models, ErrorHandler) {
        "use strict";
      
        return UIComponent.extend("kristal.apps.agreements.Component", {
      
          metadata: {
            manifest: "json"
          },
      
          init: function() {
            UIComponent.prototype.init.apply(this, arguments);
            this._oErrorHandler = new ErrorHandler(this);
            this.setModel(models.createDeviceModel(), "device");
            this.setModel(models.createFLPModel(), "FLP");
            this.getRouter().initialize();
          },
      
          destroy: function() {
            this._oErrorHandler.destroy();
            // call the base component's destroy function
            UIComponent.prototype.destroy.apply(this, arguments);
          },
      
          getContentDensityClass: function() {
            if (this._sContentDensityClass === undefined) {
              // check whether FLP has already set the content density class; do nothing in this case
              if (jQuery(document.body).hasClass("sapUiSizeCozy") || jQuery(document.body).hasClass("sapUiSizeCompact")) {
                this._sContentDensityClass = "";
              } else if (!Device.support.touch) { // apply "compact" mode if touch is not supported
                this._sContentDensityClass = "sapUiSizeCompact";
              } else {
                // "cozy" in case of touch support; default for most sap.m controls, but needed for desktop-first controls like sap.ui.table.Table
                this._sContentDensityClass = "sapUiSizeCozy";
              }
            }
            return this._sContentDensityClass;
          }
      
        });
      });
      

0 个答案:

没有答案