SAPUI5中attachMatched()和attachPatternMatched()和/或attachRouteMatched()以及attachRoutePatternMatched()之间有什么区别?

时间:2017-04-30 13:17:36

标签: sapui5

对以下SAPUI5路由方法之间的区别示例感到高兴:

sap.ui.core.routing.Route

  • attachMatched()
  • attachPatternMatched()

sap.ui.core.routing.Router

  • attachRouteMatched()
  • attachRoutePatternMatched()

API代表attachMatched()attachPatternMatched()没有任何差异。

API代表attachRouteMatched()

  

将事件处理程序fnFunction附加到此routeMatched事件   sap.ui.core.routing.Router

API代表attachRoutePatternMatched()

  

将事件处理程序fnFunction附加到routePatternMatched事件   这个sap.ui.core.routing.Router。此事件与路线类似   匹配。但它只会触发具有匹配的路线   模式,而不是其父Routes

E.g。可以使用

sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function (Controller) {
    "use strict";
    return Controller.extend("sap.ui.demo.wt.controller.Detail", {
        onInit: function () {
            var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
            oRouter.getRoute("detail").attachMatched(this._onObjectMatched, this);              
            // oRouter.attachRouteMatched(this._onObjectMatched, this);
        },
        _onObjectMatched: function (oEvent) {
            this.getView().bindElement({
                path: "/" + oEvent.getParameter("arguments").invoicePath,
                model: "invoice"
            });
        }
    });
});

sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function (Controller) {
    "use strict";
    return Controller.extend("sap.ui.demo.wt.controller.Detail", {
        onInit: function () {
            var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
            oRouter.getRoute("detail").attachPatternMatched(this._onObjectMatched, this);
            // oRouter.attachRoutePatternMatched(this._onObjectMatched, this);
        },
        _onObjectMatched: function (oEvent) {
            this.getView().bindElement({
                path: "/" + oEvent.getParameter("arguments").invoicePath,
                model: "invoice"
            });
        }
    });
});

没有区别。不要得到«但它只会触发具有匹配模式的路线,而不是其父路线。»思想attachRouteMatch()只会触发具有匹配模式的路线。

1 个答案:

答案 0 :(得分:5)

这方面的差异是:

  1. sap.ui.core.routing.Routesap.ui.core.routing.Router
  2. sap.ui.core.routing.Route attachMatchedattachPatternMatched针对特定路线发生火灾。在下面的路线«详细»:

    let oRouter = sap.ui.core.UIComponent.getRouterFor(this);
    oRouter.getRoute("detail").attachMatched(this._onObjectMatched, this);   
    
    任何路线的

    sap.ui.core.routing.Router attachRouteMatchedattachRoutePatternMatched点火:

    let oRouter = sap.ui.core.UIComponent.getRouterFor(this);            
    oRouter.attachRouteMatched(this._onObjectMatched, this);
    

    为了澄清:如果为特定路线添加了限制,sap.ui.core.routing.Router的方法将与sap.ui.core.routing.Route的方法具有相同的结果:

    _onObjectMatched: function(oEvent) {
        if (oEvent.getParameter("name") !== "detail") {
            …
        }
    }
    

    但是,sap.ui.core.routing.Router会为任何路线触发_onObjectMatched。详细路由的限制发生在带有if子句的触发方法_onObjectMatched中。 sap.ui.core.routing.Route只有在“详细信息”路线被击中时才会首先触发_onObjectMatched

    1. sap.ui.core.routing.Router attachMatched / sap.ui.core.routing.Route的{​​{1}}和attachRouteMatched的{​​{1}} / sap.ui.core.routing.Router { {1}}
    2. attachPatternMatched / sap.ui.core.routing.Route会触发匹配的路线attachRoutePatternMatched针对或子路由的任何路由触发。 attachMatched将触发指定路线的匹配。

      总结:

      • attachRouteMatched / attachMatched会触发匹配的子路由
      • attachRouteMatched触发路线的子路线
      • attachPatternMatched会触发任何匹配的子路由。即attachRoutePatternMatched / attachPatternMatched在没有父路线的情况下开火。

      <强> TL; DR:

      • attachRoutePatternMatched的具体路线。
      • 没有attachPatternMatched的具体路线。
      • attachRoutePatternMatched / sap.ui.core.routing.Route会触发任何路线。
      • sap.ui.core.routing.Router / attachMatched会触发子路由,或者不会触发父路径。