Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

sapui5 - Passing Data Between Controllers While Navigating

I want to pass data between two controllers (in addition to routing parameters) and I would like to know the correct way to do this.

For example: when I navigate to pattern /order/{id}, I do this in the view controller:

this.getRouter().navTo("order", {
  id: sOrderId
});

I want to pass additional JSON object which I don't want to be part of routing parameter.

What should I do in this case?

--edit
Wanted to add what I like to achieve with this

I want pass data from master to detail. Both master and detail page has individual routing patterns assigned. So user can land on master or detail directly. When they land on master - user can choose bunch of detail items, and navigate to first detail item, and from there navigate to other items he/she selected earlier on master. So what I want to pass is this selection from master controller to detail controller.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Note: If the intention is to pass selected keys from the main view to the detail view, see https://stackoverflow.com/a/48870579/5846045 instead.


Using Client-side Model

Usually, data are stored separately in models instead of assigned to local variables and passing them around. Model data can be then shared with anything that can access the model (e.g. View for data binding). Here is an example with a client-side JSONModel:

  1. Create a client-side JSONModel which is set on a parent ManagedObject. E.g. on Component via manifest.json:

    "sap.ui5": {
      "models": {
        "myModel": {
          "type": "sap.ui.model.json.JSONModel"
        }
      }
    }
    
  2. In controller A, set the object to pass before navigating:

    const dataToPass = /*...*/
    this.getOwnerComponent().getModel("myModel").setProperty("/data", dataToPass);
    
  3. In controller B, do something with the passed data. E.g. on patternMatched handler:

    onInit: function() {
      const orderRoute = this.getOwnerComponent().getRouter().getRoute("order");
      orderRoute.attachPatternMatched(this.onPatternMatched, this);
    },
    
    onPatternMatched: function() {
      /*Do sth with*/ this.getOwnerComponent().getModel("myModel").getProperty("/data");
    },
    

Using NavContainer(Child) Events

There are several navigation-related events such as navigate, BeforeHide, BeforeShow, etc. which contain both views - the source view (from) and the target view (to).

You can make use of the API data to pass the data. Here is an example:

  1. In controller A

    onInit: function() {
      this.getView().addEventDelegate({
        onBeforeHide: function(event) {
          const targetView = event.to;
          const dataToPass = /*...*/
          targetView.data("data", dataToPass);
        }
      }, this);
    },
    
  2. In controller B

    onInit: function() {
      this.getView().addEventDelegate({
        onBeforeShow: function(event) {
          /*Do sth with*/ this.getView().data("data");
        }
      }, this);
    },
    

Related documentation topic: Passing Data when Navigating


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...