最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to properly do route matched when switching views on UI5? - Stack Overflow

matteradmin6PV0评论

I have an SAPUI5 application with multiple pages. I have the following code on one of my view controllers.

    onInit : function () {
        var router = sap.ui.core.UIComponent.getRouterFor(this);
        router.attachRouteMatched(this.initController, this);            
    },
    
    initController: function(evt){
        
        tableControls = ViewControls.tableViewControls.apply(this);

        //update data row first
        var updateRowFlag = ViewControls.getUpdateRowFlag();
        
        if(updateRowFlag){
             var rowData = ViewControls.getRowData();
             rowData = rowData[0];
             ViewControls.updateSummaryData(rowData);
        }

        data = ViewControls.getSummaryData();
        this.setPaginationData();            
    },

What the code above intends to do is that when I navigate to this page the router will trigger the event and call initController() to all the data binding and other stuff shown above. The problem I have is that I only want it to be trigger when I go into that page. But it's also being triggered when I navigate away from this view into other views. When I do this initController() gets called again before moving away which causes a delay.

Here is the routing in my ponent.js

metadata: {
        rootView: "myApp.view.App",
        routing: {
            config: {
                routerClass: "sap.m.routing.Router",
                viewPath: "myApp.view",
                controlId: "rootControl",
                controlAggregation: "pages",
                viewType: "XML"
            },
            routes: [
                {
                    name: "page1",
                    // empty hash - normally the start page
                    pattern: "",
                    target: "page1"
                },
                {
                    name: "page2",
                    pattern: "Page2",
                    target: "page2"
                },
                {
                    name: "page3",
                    pattern: "Page3",
                    target: "page3"
                }
            ],
            targets: {
                page1: {
                    viewName: "InputsView",
                    viewId:"InputsView",
                    viewLevel: 0
                },
                page2: {
                    viewName: "TableView",
                    viewId:"TableView",
                    viewLevel: 1
                },
                page3: {
                    viewName: "DetailsView",
                    viewId:"DetailsView",
                    viewLevel: 3
                }
            }
        }

I have an SAPUI5 application with multiple pages. I have the following code on one of my view controllers.

    onInit : function () {
        var router = sap.ui.core.UIComponent.getRouterFor(this);
        router.attachRouteMatched(this.initController, this);            
    },
    
    initController: function(evt){
        
        tableControls = ViewControls.tableViewControls.apply(this);

        //update data row first
        var updateRowFlag = ViewControls.getUpdateRowFlag();
        
        if(updateRowFlag){
             var rowData = ViewControls.getRowData();
             rowData = rowData[0];
             ViewControls.updateSummaryData(rowData);
        }

        data = ViewControls.getSummaryData();
        this.setPaginationData();            
    },

What the code above intends to do is that when I navigate to this page the router will trigger the event and call initController() to all the data binding and other stuff shown above. The problem I have is that I only want it to be trigger when I go into that page. But it's also being triggered when I navigate away from this view into other views. When I do this initController() gets called again before moving away which causes a delay.

Here is the routing in my ponent.js

metadata: {
        rootView: "myApp.view.App",
        routing: {
            config: {
                routerClass: "sap.m.routing.Router",
                viewPath: "myApp.view",
                controlId: "rootControl",
                controlAggregation: "pages",
                viewType: "XML"
            },
            routes: [
                {
                    name: "page1",
                    // empty hash - normally the start page
                    pattern: "",
                    target: "page1"
                },
                {
                    name: "page2",
                    pattern: "Page2",
                    target: "page2"
                },
                {
                    name: "page3",
                    pattern: "Page3",
                    target: "page3"
                }
            ],
            targets: {
                page1: {
                    viewName: "InputsView",
                    viewId:"InputsView",
                    viewLevel: 0
                },
                page2: {
                    viewName: "TableView",
                    viewId:"TableView",
                    viewLevel: 1
                },
                page3: {
                    viewName: "DetailsView",
                    viewId:"DetailsView",
                    viewLevel: 3
                }
            }
        }
Share Improve this question edited Dec 21, 2020 at 12:32 Boghyon Hoffmann 18.1k14 gold badges93 silver badges205 bronze badges asked Jan 17, 2018 at 16:15 polarispolaris 3392 gold badges15 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Replace

// UIComponent required from "sap/ui/core/UIComponent"
var router = UIComponent.getRouterFor(this); 
router.attachRouteMatched(this.initController, this);

with

// UIComponent required from "sap/ui/core/UIComponent"
const route = UIComponent.getRouterFor(this).getRoute(<yourRouteName>);
route.attachMatched(this.initController, this);

Then it is only matched when you reached the specified route

Post a comment

comment list (0)

  1. No comments so far