Event for view leave

我与影子孤独终老i 提交于 2020-01-14 10:48:22

问题


I declared a controller for a view in my SAPUI5 application. Now I want to perform tasks when the view is left by the user.

There is already a possibility to add a callback function to attachRoutePatternMatched to perform tasks when the view is navigated by the user now I need a equivalent function to handle a leave of the view. I use a SplitContainer as parent container

onInit: function() {
  this._oRouter = this.getOwnerComponent().getRouter();
  this._oRouter.attachRoutePatternMatched(this._routePatternMatched, this);
},

_routePatternMatched: function(oEvent) {
  var that = this;
  var sRouteTargetName = oEvent.getParameter("name");
  if (sRouteTargetName === "myView") {
    // perform tasks if the view is opened by the user
  }
},

回答1:


You can try if this works:

navAway: function(viewName, callback) {
    this._oRouter.navTo(viewName);
    if(callback && typeof(callback) === "function") {
        callback();
    }
}

e.g. this.navAway("myView", function() { //doStuff });




回答2:


Presume you mean navigating backwards? If you have a back button, which presumably you must, put your actions in that function. E.g your detail/master has a navBack button in the toolbar, so put your logic in the button's event handler...




回答3:


You can achieve this with BeforeHide delegate on the NavContainer child which is often the view:

onInit: function() {
  this._navDelegate = { onBeforeHide: this.onBeforeLeave };
  this.getView()/*<-- navContainerChild*/.addEventDelegate(this._navDelegate, this);
},

onBeforeLeaving: function(event) {
  // ... do something
}, 

onExit: function() {
  // detach events, delegates, and references to avoid memory leak
  this.getView().removeEventDelegate(this._navDelegate);
  this._navDelegate = null;
},

Example: https://embed.plnkr.co/wp6yes?show=controller%2FNext.controller.js,preview%23next

  • API reference: NavContainerChild
  • API reference: sap.ui.core.Element#addEventDelegate

For other navigation related events, see documentation topics mentioned in https://stackoverflow.com/a/55649563



来源:https://stackoverflow.com/questions/30758411/event-for-view-leave

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