dotchuZ
dotchuZ

Reputation: 2651

SAPUI5 get single property from JSON-Model

I am currently trying to figure out how I can retrieve a single value from a sap.ui.model.json.JSONModel

in my main view:

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

var getConfigCallback = function(config) {
    gConfigModel.setData(config);
};

oController.getConfiguration(getConfigCallback);
console.log(gConfigModel);

in my controller:

getConfiguration : function(callback) {

    var sServiceUrl = "/sap/opu/odata/sap/xxx/ConfigurationSet('Initialize')";

    var config = {};
    callback(config);

    $.getJSON(sServiceUrl).done(function(data) {
        config = data.d;
        callback(config);
    });
},

In my console.log statement I can see that the data was successfully passed from the backend and successfully set to the JSON model. My requirement is to store the value of attribute Editable in a single variable.

console.log-statement

I already tried gConfigModel.getProperty('/'), didnt work. tried to access gConfigModel.oData was undefined .. How can I store it in a single value?

Solution Comment: If you catch data from a backend, you have to take care how long it takes. data can be available later then expected, in my case I added 1s timeout, afterwards I can access the property easily

 setTimeout(function() {
     console.log(gConfigModel.getProperty('/Editable'));
 }, 1000);

Upvotes: 2

Views: 27875

Answers (3)

Aleks Mi
Aleks Mi

Reputation: 53

First of all, thanks for the effort to find solutions of our Problems! (at least, those regarding It stuff.. :) )

I've found a solution which I think is a little bit more save because the timeout is maybe somewhat arbitrary - it would depend on the machine or the amount of data that is to be fetched?

Therefore, I am using an attachRequestCompleted function:

with sUrl_2="path-to-my-service";
var oModel_2 = new sap.ui.model.json.JSONModel(sUrl_2);

oModel_2.attachRequestCompleted(function(data) {

       //now, i can access the data stored in the oModel_2, either by getProperty, or by DOM: oModel_2.oData.d.Vendor

        gv_selLieferant = oModel_2.getProperty("/d/Vendor");
        gv_selEinkOrg = oModel_2.getProperty("/d/PurchOrg");
        gv_selEinKGru = oModel_2.getProperty("/d/PurGroup");

    });

Upvotes: 3

Haojie
Haojie

Reputation: 5713

<script src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" id="sap-ui-bootstrap" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.m"></script>

<script>
    function getConfiguration(callback) {

        var sServiceUrl = "/sap/opu/odata/sap/xxx/ConfigurationSet('Initialize')";

        var config = {};

        var data = {
            "d": {
                "_metadata": "",
                "Backup01": "01",
                "Editable": "True"
            }
        };

        setTimeout((function() {
            config = data;
            callback(config);
        })(), 2000);
    };
    var gConfigModel = new sap.ui.model.json.JSONModel();

    var getConfigCallback = function(config) {
        gConfigModel.setData(config);
        alert(gConfigModel.getProperty("/d/Editable"));
    };

    getConfiguration(getConfigCallback);
</script>

Upvotes: 2

Qualiture
Qualiture

Reputation: 4920

I wouldn't advise using the model's getData() method since it is deprecated.

A much better solution is to use gConfigModel.getProperty("/Editable") (I'm using the root slash here since your property resides in the root of your model)

In the same way, you can also set your data: gConfigModel.setProperty("/Editable", <your new value>) instead

Upvotes: 5

Related Questions