“退出”方法从何而来?记录在哪里?

问题描述

我一直遵循步骤19:重用对话框中的walkthrough tutorial。在下面的代码中,我无法弄清楚exit方法的来源。我在API reference for ManagedObject中找不到任何内容

sap.ui.define([
  "sap/ui/base/Managedobject","sap/ui/core/Fragment"
],function (Managedobject,Fragment) {
  "use strict";

  return Managedobject.extend("sap.ui.demo.walkthrough.controller.HelloDialog",{
    constructor: function(oView) {
      this._oView = oView;
    },exit: function () {
      delete this._oView;
    },open: function() {
      // ...
    }
  });
});

如果API参考中未对此进行记录,那么人们将如何知道exit可以覆盖,更重要的是为什么不覆盖destroy而不是exit呢?像这样:

  // ...
  return Managedobject.extend("sap.ui.demo.walkthrough.controller.HelloDialog",destroy: function() {
      delete this._oView;
      Managedobject.prototype.destroy.apply(this,arguments);
    },open: function() {
      // ...
    }
  });
});

解决方法

挂钩方法exit记录在 ManagedObject的子类中:sap.ui.core.Elementhttps://openui5.hana.ondemand.com/api/sap.ui.core.Element#methods/exit

Hook方法,用于在销毁之前清理元素实例。应用程序不得直接调用此hook方法,框架在元素被销毁时会调用它。
Element的子类应重写此钩子,以执行任何必要的清理。

exit: function() {
   // ... do any further cleanups of your subclass e.g. detach events...

   if (Element.prototype.exit) {
       Element.prototype.exit.apply(this,arguments);
   }
}

有关如何使用出口挂钩的详细说明,请参见第exit() Method in the documentation节。

sap.ui.base.Object> .EventProvider> .ManagedObject> sap.ui.core.Element> .Control> ...


“为什么不改写destroy?”好吧,本演练并不能解释的一件事是,在开发UI5内容时主要有两个角色:

  • 控件-/ 框架-为应用程序开发提供平台的开发者
    →覆盖受受保护的方法,例如exit
  • 应用开发人员,他们只使用高级控件和API。
    →仅应调用 public 方法,例如,如果不再需要该控件,则调用destroy

在步骤19中,通过扩展低级类(例如ManagedObject),您正在超越应用程序开发人员的角色,并为将调用{{ 1}}。