Search code examples
sapui5

Routing with Parameters - patternMatched Event Not Fired


I am working on UI5 application using Web IDE and I have created a view where I need to bind the data based on parameter received from last view. But the patternMatched event is not firing.

manifest.json

"routing": {
  "config": {
    "routerClass": "sap.m.routing.Router",
    "viewType": "XML",
    "viewPath": "oomsdisplay.pso.com.view",
    "controlId": "app",
    "controlAggregation": "pages",
    "bypassed": {
      "target": ["notFound"]
    },
    "async": true
  },
  "routes": [{
    "pattern": "",
    "name": "worklist",
    "target": ["worklist"]
  }, {
    "pattern": "ZISOHSet/{objectId}",
    "name": "object",
     "target": ["object"]
  }, {
    "pattern": "ZISOHSet/{objectId}",
    "name": "payobject",
    "target": ["payobject"]
  }],
  "targets": {
    "worklist": {
      "viewName": "Worklist",
      "viewId": "worklist",
      "viewLevel": 1
    },
    "object": {
      "viewName": "Object",
      "viewId": "object",
      "viewLevel": 2
    },
    "objectNotFound": {
      "viewName": "ObjectNotFound",
      "viewId": "objectNotFound"
    },
    "notFound": {
      "viewName": "NotFound",
      "viewId": "notFound"
    },
    "payobject": {
      "viewName": "PayObject",
      "viewId": "payobject",
      "viewLevel": 2
    }
  }
}

Component.js

init: function() {
  UIComponent.prototype.init.apply(this, arguments);
  // ...
  this.getRouter().initialize(); // create the views based on the url/hash
},

Now, I have created a view. When I press the button on view, my second view i.e. PayObject will be called. Here is my code on button press of view one:

fViewPayment: function(oEvent) {
  this.getRouter().getTargets().display("payobject", {
    objectId: "MyParameterhere"
  });
},

Now, here is my PayObejct view's init handler with object matched handler. But it is not working.

onInit: function() {
  // ...
  var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
  oRouter.getRoute("payobject").attachPatternMatched(this._onObjectMatched, this);
},

_onObjectMatched: function(oEvent) { // this event handler is not firing
  var sObjectId = oEvent.getParameter("arguments").objectId;
  sap.m.MessageBox.show(sObjectId);
  // ...
},

Solution

  • If you have two routes with the same pattern, only the first one will be matched in the Router.

    {
       "pattern": "ZISOHSet/{objectId}",
       "name": "object",
       "target": ["object"]
    }, {
       "pattern": "ZISOHSet/{objectId}",
       "name": "payobject",
       "target": ["payobject"]
    }
    

    So in this case "object" will me matched. Change the patter of one of your routes and try again.

    Furthermore, use navTo() when navigating:

    this.getRouter().navTo("payobject", {
      objectId: "MyParameterhere"
    });