1

In the following code I try to destroy JSON model if it exists:

if(sap.ui.getCore().getModel("modelId")){
    console.log(sap.ui.getCore().getModel("modelId"));
    sap.ui.getCore().getModel("modelId").destroy();
};

but the above model is not destroyed.

The above model is set in another function and it looks like this:

var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(oData);
sap.ui.getCore().setModel(oModel, "modelId");
console.log(sap.ui.getCore().getModel("modelId"));

The logs are:

the 1st snippet (where I try to destroy the model):

C.extend.constructor {mEventRegistry: Object, oData: Object, bDestroyed: false, aBindings: Array[0], mContexts: Object…}

the second snippet (where the model is set):

EventProvider sap.ui.model.json.JSONModel

What am I missing here? Why the logs are so different?

The main problem in this - is that I try to destroy that model, but it doesn't work.

keshet
  • 1,716
  • 5
  • 27
  • 54
  • 1
    Have you made sure that "sap.ui.getCore().getModel("modelId")" actually returns a model? Does this object have the property "destroy()"? You are able to do so with console.log(sap.ui.getCore().getModel("modelId")); in case you didn't know. – OddDev May 04 '15 at 08:37

1 Answers1

1

I checked the API here and it says that a Model implementation may interfere with the destroy function. I had the same result with my Model, when I tried to delete it, all that got deleted were the Bindings, but not the whole Model.

var test = sap.ui.getCore().getModel("partnerDaten");
console.log(test);
if(test !== undefined){
    sap.ui.getCore().getModel("partnerDaten").destroy();
    this.getView('bearbeiten').getModel("partnerDaten").refresh(true);
    console.log(test);
};

These are the Console logs.

C.extend.constructor {mEventRegistry: Object, oData: Object, bDestroyed: false, aBindings: Array[46], mContexts: Object…}
C.extend.constructor {mEventRegistry: Object, oData: Object, bDestroyed: true, aBindings: Array[0], mContexts: Object…}

As you can see from the code sample above. I will keep looking but I guess it is not possible to delete the whole model.

Osamah Aldoaiss
  • 238
  • 4
  • 16
  • 1
    Found similar thrad on SAP blog - https://archive.sap.com/discussions/thread/3534349. unset the model from the core works - While setting the model if you had not given any name, then use `sap.ui.getCore().setModel();` This will overwrite the old model. Since you have a used a name, pass null this time. That is `sap.ui.getCore().setModel(null, 'userModel');` – user557657 Mar 03 '17 at 13:49