承诺不起作用(对我来说) [英] Promises do not work (for me)

查看:47
本文介绍了承诺不起作用(对我来说)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能我在这里遗漏了一些基本的东西.场景是这样的:

Probably I'm missing something basic here. Scenario goes like this:

在我的应用程序(App.controller.js,onInit 函数)的根视图控制器中,我正在对命名模型执行一系列读取操作,以便为主要为以下目的准备额外的数据由格式化程序在后续视图中使用.我所做的是使用承诺来确保数据准备就绪.

Within the root view controller of my application (App.controller.js, onInit function) I'm performing a series of read operations on a named model in order to have additional data ready mainly for use by formatters in subsequent views. What I have done is using promises to make sure that data will be ready.

问题是应用程序偶尔崩溃时,模式""的初始视图(在我的例子中是Worklist.view.xml)在根视图之后加载 (App.view.xml) 因为格式化函数找不到预期的数据.添加了几个断点并观察调试器:

Problem is that application occasionally crashes when the initial view with pattern "" (Worklist.view.xml in my case) is loaded after the root view (App.view.xml) because the formatter function can't find the data expected. Added a few break-points and observed the debugger:

  1. 在第一个读取函数处停止
  2. 在最后的 promises 条件处停止
  3. 在后续视图中的格式化程序函数处停止(并抛出错误)
  4. 在oData读取(解析)成功函数中停止
  5. 由于上面抛出的异常,页面随后冻结

正常"流程,当应用程序没有崩溃时是这样的:

"Normal" flow, when application doesn't crash goes like this:

  1. 在第一个读取函数处停止
  2. 在最后的 promises 条件处停止
  3. 在oData读取(解析)成功函数中停止
  4. 在 readyToGo 函数处停止(它只是为了临时调试)
  5. 在后续视图的格式化函数处停止
  6. 页面显示,一切井然有序

从以上来看,我得出的结论是,我的承诺是行不通的.有什么想法吗?

Judging by the above, I have reached the conclusion that my promises do not work. Any ideas?

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:

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"
  }
}

组件.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;
    }

  });
});

推荐答案

这一切都是关于将功能从根视图移到 component.js 并将路由器初始化放入承诺条件中,谢谢专家

It was all about moving functionality from the root view to component.js and putting the router initialization into the promise conditions, thank you experts

这篇关于承诺不起作用(对我来说)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆