cancel
Showing results for 
Search instead for 
Did you mean: 

Copy and store the default model

09-28-2018 10:36 AM
942 views 5 comments
0 Likes
SAP Managed Tags
Subscribe

Hi, I have a created a default model and when I start my application, I want to copy it to a second model and then use it in a different function to reset everything. Here what I have done, on my InitFunction:

console.log("Data ", data);
copyModel = $.extend({}, data);

Data is my original model. I have declared the copyModel as a global variable.

Then I have the resetFunction:

resetP: function(){
this.getView().setModel(new JSONModel(copyModel));
console.log("CopyModel ", copyModel); }

The code seems to run, but I have a weird problem. When I first click the reset Button, copyModel is same with the original model (data). The second time I ran the resetFunction, copyModel is not the same with the original. It has been overwritten with the existing one the moment I press the button. Can anyone help me with that? Thank you!

0 Likes

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Likes

Unfortunately, none of these two ways worked. As it's written on Stackoverflow and works for me:

resetP: function(){
this.getView().getModel().updateBindings();
}

SergioG_TX
SAP Champion
SAP Champion
0 Likes

try cloning the original object like this instead of the $.extend function

var copyObject = JSON.parse(JSON.stringify(originalObject));
jorge_cabanas
Participant
0 Likes

It should work because the pointer is not assigned in this way. So when the original data is modified (whenever), the copy will never be modified.
Good point.

jorge_cabanas
Participant
0 Likes

I don't understand really well what are you trying to ask...

But I'm gonna play... I think that the problem is in new JSONModel:

this.getView().setModel(new JSONModel(copyModel)); 

What is DATA? Json model? oData Model?

Former Member
0 Likes

Yes, data is my original JSON model from a form which is being created when I first open my app. I want to have a copy of this model for using it when I am resetting my form. The problem is that at first I keep a right copy and when I run the reset function for a second time, the copy of the model is changed.