cancel
Showing results for 
Search instead for 
Did you mean: 

sap.m.Dialog escapeHandler how-to

Attila
Active Participant

Hello Experts,

there is a new property of sap.m.Dialog, called escapeHandler. Does anyone know, how-to use it? It is available since 1.44. We're on 1.44.7, but could not make it working. It is a property and not an event handler btw.

The Dialog.fragment.xml part:
<Dialog title="{i18n>chooseWS}" contentHeight="100%" contentWidth="100%" stretch="true" verticalScrolling="false" escapeHandler="mayTheForceBeWithMe">

The Controller:
this._oDialog = sap.ui.xmlfragment("xxx.xxx.xxxx.xxx.Dialog", this); this._oDialog.setModel(this.getModel("i18n"), "i18n");
this._oDialog.open();
...
mayTheForceBeWithMe: function(oPromise) {
if (!this.oFilterEntry || this.oFilterEntry.workspace == "") {
oPromise.reject(); } else {
oPromise.resolve();
}
},
...

Function mayTheForceBeWithMe is not invoked.

Thank you

Accepted Solutions (1)

Accepted Solutions (1)

former_member228602
Contributor
0 Kudos

Hello Atilla,

I checked the issue and i found that if the dialog is created in the XML Fragment then it does not work. Where as if it is created in javascript controller it works. I checked the code of Dialog controller and this is the code

if (typeof oEscapeHandler === 'function') {
				// create a Promise to allow app developers to hook to the 'escape' event
				// and prevent the closing of the dialog by executing the escape handler function they defined
				new window.Promise(function (resolve, reject) {
					oPromiseArgument.resolve = resolve;
					oPromiseArgument.reject = reject;


					that.currentPromise = oPromiseArgument;


					oEscapeHandler(that._getPromiseWrapper());
				})
					.then(function (result) {
						that.close();
					})
					.catch(function () {
						jQuery.sap.log.info("Disallow dialog closing");
					});
			} else {


				this.close();
			}


          

So there the oEscapeHandler needs to be function . I think this could be a bug from why checks for the type , but yes the way for you to make it work is using in javascript controller .

Thanks and Regards,

Veera

Answers (4)

Answers (4)

boghyon
Product and Topic Expert
Product and Topic Expert

The escapeHandler can now be assigned in XML: https://stackoverflow.com/a/64909600/5846045 (Since 1.86)

Attila
Active Participant

Hi Boghyon,

good to hear, I'll keep this in mind for the next freestle app implementation.
Thanks

vidyag
Explorer
0 Kudos

Hi boghyon.hoffmann,

Thank you for your detailed explanation, could you please clarify few doubts since I'm new to UI5 coding:
1. Why do we have use the dot '.' infront of the controller method name in order to call it, while other methods in the view works fine without it?

2. Could you please provide a sample code snippet on how pEscapePending.resolve() or pEscapePending.reject() is called?

Thanks in advance.

Epena
Participant

Hi Attila, as Veeraraghavan explained, onEscapeHandler is not working properly on XML fragments, I just struggled with this issue myself. However I wanted to post a workaround in case anyone else has this problem:

1.- In the Dialog.xml.fragment, assign an ID to your Dialog:

<Dialog id="dlg" title="{i18n>chooseWS}" contentHeight="100%" contentWidth="100%" stretch="true" verticalScrolling="false" escapeHandler="mayTheForceBeWithMe">

2.- After fragment instanctiation, attach an onClose event handler to your Dialog "dlg":

if(!this._oDialog){
     this._oDialog = sap.ui.xmlfragment("xxx.xxx.xxxx.xxx.Dialog", this); this._oDialog.setModel(this.getModel("i18n"), "i18n");
        var that = this;
        var dlg = sap.ui.getCore().byId("dlg");
	dlg.attachAfterClose(function() {	
	    /*Here implement your logic to determine if the dialog should be closed or not, IE: if an item in your dialog was selected or not, or whether an input has been filled with data or not. In case you determine it should not be closed, then call this._oDialog until your condition is met.*/
	});

}

Hope it helps.

Regards.

former_member757057
Discoverer
0 Kudos
Thank you very much, it has served me wonderfully!
Ícono de validado por la comunidad
former_member757873
Discoverer
0 Kudos

Thanks for sharing. It worked for me

0 Kudos

Hi,

If you have created the dialog via XML fragment you can use the following workaround to handle the escape

var oFragmentId = "myFragId", oFragmentName = "com.abc.fragment.myPopup";
if (!this.myPopup) {
/* _createFragment is a base controller function i made to create an xml fragment */
this.myPopup= this._createFragment(oFragmentId, oFragmentName);
/* Since i use XML view ,We will attach the escape event handler in controller level */
this.myPopup.setEscapeHandler(function(o) { o.reject(); });

Attila
Active Participant

Thank You Ramees. Solved with JS based approach, but next time I am gonna use your approach.

former_member365727
Active Contributor
0 Kudos

Sample code is avilable in explored app

Attila
Active Participant
0 Kudos

Thank you for the snippet Srikanth