2

I want to pass a JSON object from one controller to another. How do I do that?

Or can I pass a oModel to the other view? If so how do I do that?

Thanks

Zee
  • 8,420
  • 5
  • 36
  • 58
  • Does this answer your question? [Passing Data Between Controllers While Navigating](https://stackoverflow.com/questions/48831967/passing-data-between-controllers-while-navigating) – Boghyon Hoffmann Jul 17 '21 at 12:18

4 Answers4

5

If you store your data in a global model:

var oData  = <your JSON data>;
var oModel = new sap.ui.model.json.JSONModel(oData);
sap.ui.getCore().setModel(oModel);

it can then be accessed from any other controller/view in your application context

Qualiture
  • 4,900
  • 7
  • 27
  • 38
2

one solution is that you use sap.ui.core.EventBus. Basically you can pass any object from one controller to another including JSONModel using subscribe and publish.

Haojie
  • 5,665
  • 1
  • 15
  • 14
0

To add to Qualiture's answer:

If you wanted to attach a specific model to a specific view, which your question sounds like a little, you can do just that by going:

view.setModel(oModel);

Cheers Michel

michel luther
  • 279
  • 1
  • 2
  • 11
0

yes,you can pass string,object and Array from one view to another through the Navigation.

    enter code here
enter code here for controller
    handleLinkPress: function(oEvent) {
            try {

                var array = [];

                var obj = {

                    "Delivery": "80000095",
                    "DelvNo": "80000095",
                    "Date": "2018-01-04T00:00:00.000Z",
                    "Priority": "00",
                    "LoadDate": "2018-01-04T00:00:00.000Z",
                    "PickDate": "2018-01-04T00:00:00.000Z",
                    "ShippingPoint": "0001",
                    "Route": "",
                    "LoadingPoint": "01",
                    "ShipTo": "CUSTOMER",
                    "SoldTo": "CUSTOMER"
                };
                array.push(obj);
                if (obj !== null) {
                    sap.ui.core.UIComponent.getRouterFor(this).navTo("Route", {
                        RouteData: JSON.stringify(array)
                    });

                }

            } catch (e) {

                console.error("Error : " + e.message);
                sap.m.MessageBox.show(e.message, {
                    icon: sap.m.MessageBox.Icon.ERROR,
                    title: "Error",
                    actions: [sap.m.MessageBox.Action.OK],
                    defaultAction: sap.m.MessageBox.Action.OK,
                    styleClass: "sapUiSizeCompact",
                    contentWidth: "150px"

                });
            }

        }

Code for manifest.json
    {
    "pattern": "Route/{RouteData}",
    "name": "Route",
    "targetControl": "masterView",
    "viewId": "Route",
    "viewName": "Route"
    }

code for the controller where you get that Array which you want pass 

    handleCloseRoutePress: function(oEv) 
{

             var sRouteData=JSON.parse(oEv.getParameter("arguments").RouteData);


        }
Jens
  • 67,715
  • 15
  • 98
  • 113