cancel
Showing results for 
Search instead for 
Did you mean: 

Passing Json Model from one controller to another controller

Former Member
0 Kudos

Hi,

We are extending the Price and Availability app.

We have two views S2 and S3.

S2 is a search screen where we input some values and on click of a button, we need to show the gateway results in S3 which is a detail screen

We have a S2 controller where in the action handler, we are calling a gateway service and creating a json model for the odata results.

Now, we want to use the results in S3 view. How to get the json model in S3. We are aware that we can get it in S3 Controller using sap.ui.getCore.getModel('Modelname');

But our question is in which method of controller, I need to call the getCore statement. I tried in init() method of S3 but it is called before the action handler of S2.

My requirement is I need to bind the results to S3 view which are retreived in S2 controller.

I am attaching the files of S2 and S3 for your reference. Please help.

Regards

V

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member182372
Active Contributor
0 Kudos

this.oRouter.attachRouteMatched($.proxy(function(oEvent) {                                              

sap.ui.getCore().getModel("MaterialAvailabilityModel");

}, this), this);

binding format is "MaterialAvailabilityModel>/PATH"

Former Member
0 Kudos

Hi Maksim Ranshchynski,

The init method of S3 Controller where I am supposed to place your code is called before my action handler of S2 Controller. As a result of this, it is giving me an error when I use the sap.ui.getCore().getModel("MaterialAvailabilityModel");

Regards

V

former_member182372
Active Contributor
0 Kudos

you do

this.oRouter.navTo("detail", !jQuery.device.is.phone);

ar the end of the method, that should fire a routeMatched event

this.oRouter.attachRouteMatched($.proxy(function(oEvent) {                                              

//This will be executed when navTo is called

}, this), this);

As alternative you can fire event once data is loaded

in S2 search event handler after model is created and stored

var oComponent = sap.ui.core.Component.getOwnerComponentFor(this.getView());

oComponent.getEventBus().publish("app", "search", { model : oMaterialAvailability} );

in S3 in onInit


var oComponent = sap.ui.core.Component.getOwnerComponentFor(this.getView());

oComponent.getEventBus().subscribe("app", "search", this._search, this);

define

_search: function(sChannel, sEvent, oData){

//oData contains oModel with oMaterialAvailability

}

But again, routerMatched should work

Former Member
0 Kudos

Hi Maksim Ranshchynski,


routeMatched() is working only for the first time when the view is called.


But if the user provided the input data again with new values and clicks on the button the routeMatched() was not getting called for the second time. In such cases, what is the best method to call the gateway service again.


Regards

V

former_member182372
Active Contributor
0 Kudos

told you, use events

Former Member
0 Kudos

As maksim said, routePatternMatched event gets triggered everytime you navigate. For example if you are navigating from s2 to s3 , then in s3 controller you can do this.

onInit: function(){

  this.router = sap.ui.core.UIComponent.getRouterFor(this);

  this.router.attachRoutePatternMatched(this._handleRouteMatched, this);

  },

  _handleRouteMatched : function (evt) {

// Write your logic here

sap.ui.getCore().getModel("MaterialAvailabilityModel");

}

Former Member
0 Kudos

Hi Indrajith,

handleRouteMatched() is getting called only for the first time.

Its not getting called everytime I navigate to S3 using navTo().

Do we need do any special configurations for this.

Regards

V

former_member182372
Active Contributor
0 Kudos

try

in onInit


this._oRouter.attachRouteMatched(this._handleRouteMatched, this);

...

_handleRouteMatched: function (evt) {

}

Former Member
0 Kudos

Hi Maksim Rashchynski,

I tried this option as well, but it is getting called only for the first time. Its not getting called everytime.

Regards

V

former_member182372
Active Contributor
0 Kudos

share your Component's metadata

Former Member
0 Kudos

Hi Maksim Rashchynski,

Please find the attached files.

I attached the component.js, S2Controller, S3Controller.

Regards

V

former_member182372
Active Contributor
0 Kudos

in S2


search: function (oEvent) {

  var that = this;

  var oModel =  new sap.ui.model.odata.ODataModel("/sap/opu/odata/sap/ZXX_PRICE_AVAIL_SRV_01/");

  oModel.read("MaterialAvailabilitySet", null, ["$filter=simMatnr eq '000000000088242626' and simWerks eq 'SLMO' and simKunag eq '100412' and simKunwe eq '8000842'"], true,

  function(oData, oResponse) {

    var oMaterialAvailability = new sap.ui.model.json.JSONModel(oData.results);

    sap.ui.getCore().setModel(oMaterialAvailability, "MaterialAvailabilityModel");

    this.oRouter.navTo( "detail", !jQuery.device.is.phone);

  }

  );

}

Former Member
0 Kudos

If I call this.oRouter.navTo( "detail", !jQuery.device.is.phone); inside function(oData, oResponse) I am getting the below error


Cannot read property 'navTo' of undefined


Regards

V

former_member182372
Active Contributor
0 Kudos

my bad,

that.oRouter.navTo( "detail", !jQuery.device.is.phone);

Former Member
0 Kudos

Its still calling only for the first time.

former_member182372
Active Contributor
0 Kudos

i dont really know the reason, hard to say as it is an extension and without original component hard to guess. try to fire an event as i suggested earlier

in S2 search event handler after model is created and stored

var oComponent = sap.ui.core.Component.getOwnerComponentFor(this.getView());

oComponent.getEventBus().publish("app", "search", { model : oMaterialAvailability} );

in S3 in onInit


var oComponent = sap.ui.core.Component.getOwnerComponentFor(this.getView());

oComponent.getEventBus().subscribe("app", "search", this._search, this);

define

_search: function(sChannel, sEvent, oData){

//oData contains oModel with oMaterialAvailability

}

Former Member
0 Kudos

Will try this option.

I have a question in RouteAttached. Will this method be called everytime with navTo() only when the model gets refreshed?

Any such criteria exists so that it will be called everytime.

Former Member
0 Kudos

I found the root cause why oRouter.attachRouteMatched is not getting called.

We are not able to see the back navigation button in when we run the application in desktops and tablets.

In mobile devices, we are able to see the back navigation button and the method is getting called.

Any idea on why the nav button is not visible in desktops and tablets.

Regards

V