javascript - "setInitialFocusId" error after _oDialog.destroy() - Stack Overflow

I get the error following error when trying to open a Dialog fragment a second time after calling this.

I get the error following error when trying to open a Dialog fragment a second time after calling this._oDialog.destroy():

Uncaught TypeError: Cannot read property 'setInitialFocusId' of null

My problem is like the problem stated here: How to clear dialog/xmlfragment content after close? However, the solution apparently just seems to be "Don't use the property setInitialFocus", which I do not use anywhere in my code.

Controller

openDialog: function() {
  if (!this._oDialog) {
    this._oDialog = sap.ui.xmlfragment("myFragmentPath", this);
    this.getView().addDependent(this._oDialog);
  }
  this._oDialog.open();
},

onExit: function () {
  if (this._oDialog) {
    this._oDialog.destroy();
  }
},

afterClose: function () {
  if (this._oDialog) {
    this._oDialog.destroy();
  }
},

handleClose: function (oEvent) {
  this._oDialog.close();
}

Dialog Fragment

<Dialog xmlns="sap.m" afterClose=".afterClose">
  <!-- ... -->
</Dialog>

Main XML View

<Button press=".openDialog" />

Additional info:

  • The error message occurs in the Controller line when this._oDialog.open(); is called.
  • I am using the sap library version 1.60.1.

I get the error following error when trying to open a Dialog fragment a second time after calling this._oDialog.destroy():

Uncaught TypeError: Cannot read property 'setInitialFocusId' of null

My problem is like the problem stated here: How to clear dialog/xmlfragment content after close? However, the solution apparently just seems to be "Don't use the property setInitialFocus", which I do not use anywhere in my code.

Controller

openDialog: function() {
  if (!this._oDialog) {
    this._oDialog = sap.ui.xmlfragment("myFragmentPath", this);
    this.getView().addDependent(this._oDialog);
  }
  this._oDialog.open();
},

onExit: function () {
  if (this._oDialog) {
    this._oDialog.destroy();
  }
},

afterClose: function () {
  if (this._oDialog) {
    this._oDialog.destroy();
  }
},

handleClose: function (oEvent) {
  this._oDialog.close();
}

Dialog Fragment

<Dialog xmlns="sap.m" afterClose=".afterClose">
  <!-- ... -->
</Dialog>

Main XML View

<Button press=".openDialog" />

Additional info:

  • The error message occurs in the Controller line when this._oDialog.open(); is called.
  • I am using the sap library version 1.60.1.
Share Improve this question edited Jan 16, 2019 at 10:38 Boghyon Hoffmann 18.1k14 gold badges93 silver badges205 bronze badges asked Jan 16, 2019 at 10:13 sandboxjsandboxj 1,2544 gold badges24 silver badges49 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7
if (this._oDialog) {
  this._oDialog.destroy();
  this._oDialog = null; // make it falsy so that it can be created next time
}

After close, the dialog is destroyed in your code. However, the this._oDialog is still there.

Since this._oDialog is not a falsy value but just a destroyed dialog instance, there is no new Dialog created in openDialog() second time. Hence you're trying to open a destroyed dialog.

When the dialog is destroyed, its internal oPopup is set to null, which explains the error message.


⚠️ Note

  1. There is usually no need to destroy the dialog after closing. When the view gets destroyed, the dialog will be destroyed automatically since the fragment is dependent to the view. If the intention was to reset data values, try unbinding properties instead of destroying and recreating the entire fragment every time which is quite costly.

  2. Since UI5 1.56, the factory function sap.ui.xmlfragment is deprecated because it fetches the fragment via sync XHR (blocking the main thread). Use one of the new asynchronous APIs.

  3. A simpler option is to add the fragment declaratively in your view definition with <core:Fragment fragmentName="..." type="XML" /> to the <dependents> aggregation of a certain control. Like in this sample.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745342322a4623384.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信