cancel
Showing results for 
Search instead for 
Did you mean: 

JSON model error in another view when refreshing browser

former_member186566
Active Participant
0 Kudos

Hi,

I have created a JSON model and bind the same into sap.ui.getCore().setModel.

var json = {};

json.plant = plant;

json.sloc = sloc;

json.material = material;

this.materialset.setData(json);

sap.ui.getCore().setModel(this.materialset,"materialset");

and the same is accessed in other view

var materialset=sap.ui.getCore().getModel("materialset");

var mSet=materialset.getData()

var fplant=mSet.plant;

var fsloc=mSet.sloc;

var fmat=mSet.material;

Im able to get all the data from JSON model.

But when i refresh the browser from 2nd view, its throwing error message in

materialset.getData()

It seems the data which I passed from 1st view got removed by browser refresh.

Wont it be reside in object even after browser refresh or is it the usual functionality ?

I need to get the values even after page refresh. help me to get a solution.

Thanks

Yokesvaran Kumarasamy

Accepted Solutions (1)

Accepted Solutions (1)

Abdul_Waheed
Contributor
0 Kudos

You should set model at application level.

Are you setting model in 1stview or Component.js ?

former_member186566
Active Participant
0 Kudos

Hi Abdul Waheed

Thanks for your reply.

Initially it was in 1st view. Now I changed that into components.js.

And i have set empty values in component.js

var materialset = new sap.ui.model.json.JSONModel();

var json = {};

json.plant = '';

json.sloc = '';

json.material = '';

json.mmbFlag = '';

materialset.setData(json);

materialset.setDefaultBindingMode("TwoWay");

sap.ui.getCore().setModel(materialset,"materialset");

Later in 1st view im giving values to the same model

this.materialset =sap.ui.getCore().getModel("materialset");

var json =this.materialset.getData();

json .plant = plant;

json .sloc = sloc;

json .material = materialList;

json .mmbFlag = getsegbtnId_value;

//this.materialset.setData(mSet);

this.materialset.updateBindings(true);

this.materialset.refresh();

When im navigating to 2nd view its working fine. But again when i hit F5 refresh, its not recognizing the data and its fetching empty data from component.js

Any idea where i did mistakes?

Regards

Yokesvaran Kumarasamy

Abdul_Waheed
Contributor
0 Kudos

You should load first view wen your application reloads.

or

Use routing to pass data from first view to second view with URI paramaters

former_member186566
Active Participant
0 Kudos

Hi Abdul Waheed

2n option is opted out. Bcz my second view is a master and detail page. When end user select list item from master page, URI parameters and detail page will change. So i cant set URI parameters.

I will try the 1st option.

But wts wrong with my code?

cant we change the JSON model data which is declared in component.js ?

Thanks & Regards

Yokesvaran Kumarasamy

Abdul_Waheed
Contributor
0 Kudos

//this.materialset.refresh();

sap.ui.getCore().byId("YOUR_VIEW_ID").getModel().refresh(true);

Try this

junwu
Active Contributor
0 Kudos

if your second view can be accessed directly, you have to take care of that case. initialize the model properly in that situation.

sebastianraemsch
Active Participant
0 Kudos

Hi,

The solution I proposed in http://scn.sap.com/thread/3850447 works only if you set your model in component.js or if view 1 is loaded before view 2.

So maybe you have to tranfer the coding from view 1 to a helper function. In view 2 you should check if the model is initialized correctly and if not call the helper function.

Best regards,

Sebastian

former_member186566
Active Participant
0 Kudos

Hi Sebastian Raemsch

Yes, i have set the Json model in components (you can see the code in above thread) and im changing the value of JSON model in View1 and passing to View 2. But its not working on refresh

Can you explain whats value helper functions and how to achieve it?

Thanks & Regards

Yokesvaran Kumarasamy

former_member186566
Active Participant
0 Kudos

Hi Jun Wu,

I cant access the 2nd view directly, based on the input from 1st view im doing some functionalities in 2nd view.

So somehow i need to pass data from 1st view to 2nd view.

Regards

Yokesvaran Kumarasamy

junwu
Active Contributor
0 Kudos

when you navigate to view 2, did u change the url? when you refresh the page, basically you are accessing that view directly.

former_member186566
Active Participant
0 Kudos

Hi,

Declaring everything in URI parameter is the right solution.

Additionally, i have added 2 entities in component.js so URI will have two sets of entity, which i execute again when page refresh.

subroutes: [{

pattern: "{entity}/{data}",

name: "detail",

view: "Detail",

//add target aggregation to detail view

targetAggregation: "detailPages"

}]

},

Regards

Yokesvaran Kumarasamy

Answers (1)

Answers (1)

prashil
Advisor
Advisor
0 Kudos

Hi Yokesvaran,

JSON model is client model, so when you refresh the client, it also loses all the data it holds.

This is the reason when you try to access 2nd view, you run into error.

To best resolve the issue, as someone previously mentioned that you have to shared the data either through context, which will actually exchange the data with the second view in the request itself or create the model during initialization.

The model has to be set in such method which will always be called when you launch the view.

Thanks

Prashil

former_member186566
Active Participant
0 Kudos

Hi Prashil Wasnik

Thanks for you reply.

Can you explain how to share the data through context?

is that mean, i have to store somewhere in backend and then we can fetch and use in other view ?

Regards

Yokesvaran Kumarasamy