How to pass JSON object from one controller to another (/ view to view)?

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

Or is it possible to pass oModel to another view? If so, how do you do it?

thank

+3


source to share


4 answers


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 be accessed from any other controller / view in the context of your application.

+4


source


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



+2


source


To add to Qualiture's answer:

If you want to attach a specific model to a specific view, which your question sounds a bit like, you can do so by going:

view.setModel(oModel);

      

Cheers Michelle

0


source


yes, you can pass string, object and array from one view to another via 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);


        }

      

0


source







All Articles