问题
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 asexit
. - And application developer who purely consumes high level controls and APIs.
→ Should call public methods only, such asdestroy
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