Skip to Content

Copy and store the default model

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!

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

3 Answers

  • Sep 28, 2018 at 09:53 AM

    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?

    Add comment
    10|10000 characters needed characters exceeded

    • 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.

  • Sep 28, 2018 at 12:53 PM

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

    var copyObject = JSON.parse(JSON.stringify(originalObject));
    Add comment
    10|10000 characters needed characters exceeded

  • Oct 01, 2018 at 08:09 AM

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

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

    Add comment
    10|10000 characters needed characters exceeded