Where does the “exit” method come from? Where is it documented?

三世轮回 提交于 2021-01-27 21:01:10

问题


I've been following the walkthrough tutorial on Step 19: Reuse Dialogs. In the code below, I cannot figure out where the exit method comes from. I could not find anything in the 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() {
      // ...
    }
  });
});

If it is not documented in the API reference, how would someone know that exit is available to override and, more importantly, why not override destroy instead of exit? Something like:

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

回答1:


The hook method exit is documented in ManagedObject's subclass sap.ui.core.Element: https://openui5.hana.ondemand.com/api/sap.ui.core.Element#methods/exit

Hook method for cleaning up the element instance before destruction. Applications must not call this hook method directly, it is called by the framework when the element is destroyed.
Subclasses of Element should override this hook to implement any necessary cleanup.

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

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

For a more detailed description how to to use the exit hook, see Section exit() Method in the documentation.

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


"Why not override destroy instead?" Well, one thing that the walkthrough doesn't explain is that there are mainly two roles when developing UI5 content:

  • Control-/ framework-developer who provides a platform for application development
    → Overrides protected methods, such as exit.
  • And application developer who purely consumes high level controls and APIs.
    → Should call public methods only, such as destroy if the control is no longer needed.

In step 19, by extending a low-level class such as ManagedObject, you're crossing the app developer role and providing a hook method for app developers who would call myHelloDialog.destroy().



来源:https://stackoverflow.com/questions/63517610/where-does-the-exit-method-come-from-where-is-it-documented

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!