Skip to Content

How to make JSON model read only?


In my SAPUI5 app, I have a json model (defined in the manifest) getting it's data from a locally stored json file.

"oDefaultModel": {
	"type": "sap.ui.model.json.JSONModel",
	"uri": "model/defaultModel.json",
	"settings": {
		"defaultBindingMode": "OneTime"

This model has some default values for stuff which should always remain static. I then have a custom control that is bound to data in the backend system, and I read the data from the backend

var oData = this.getBinding("data");
	if (oData) {
		var aData = oData.getContexts().map(function (oContext) {
		return oContext.getObject();

I then merge the data from the backend together with the default model data using a loop and replace the default values with the data from the backend where necessary.

This works fine on the first pass... but as soon as i change the context, i.e. click on another item in my list, it would appear as though the default model has been updated! now the default model contains the values from the last context instead of the default values.

How do I prevent this default model from being updated, i.e. make it read only?


Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Posted on Jun 17, 2019 at 07:02 PM

    if it is not a big deal, just make a copy at the beginning

    stringify then parse

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 19, 2019 at 10:28 AM


    What you could do is adding a "default" and a "current" properties to your .json file.

    e.g. instead of writing to the .json file :

    { "homepage": "" }

    Write :

    "default": {
    "homepage": ""
    "current": {}

    In you bindings, you will have to change the path, e.g.: "{dummy>/current/homepage}" instead of "{dummy>/homepage}"

    During the initialization, copy the data from default to current :

    var oDefault = this.getView().getModel("dummy").getProperty("/default");
    this.getView().getModel("dummy").setProperty("/current", $.extend({}, oDefault));

    Then, when you need to merge the default values with the values retrieved from the backend :

    var oNew = {
    homepage: ""
    }; // data coming from the backend
    var oDefault = this.getView().getModel("dummy").getProperty("/default");
    this.getView().getModel("dummy").setProperty("/current", $.extend({}, oDefault, oNew));

    That way, /default/homepage stays to "" and only "/current/homepage" is updated to "".

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.