Reputation: 1826
I define json model in onInit()
function of the controller:
var oSelectedFields = {};
oSelectedFields.counter = 0;
var oSelectedFieldsModel = new sap.ui.model.json.JSONModel();
oSelectedFieldsModel.setData(oSelectedFields);
sap.ui.getCore().setModel(oSelectedFieldsModel, "selectedFieldsCounter");
console.log("oninit(). ",oSelectedFields); //(1)
Then, in one of the functions I want to get this model's data, use it and update it (the function is called on press
event):
function(oEvent){
var oSelectedFields = sap.ui.getCore().getModel("selectedFieldsCounter").getData();
console.log(oSelectedFields);//(2)
oSelectedFields.counter++;
//rest of the function's code here
}
Line (1) displays oninit(). Object {counter: 0}
and line (2) displays Object {}
.
If I call the function for the second time, line (2) displays Object {counter: NaN}
The question is why I cannot get the data of the model?
Upvotes: 0
Views: 1492
Reputation: 1826
I had a variable interference error in the model declaration.
After the model declaration I declared another model with empty object.
By error, I assigned that empty object to the first model.
Upvotes: 0
Reputation: 849
I got it working. Check if this link is of use Example
var oSelectedFields = sap.ui.getCore().getModel("selectedFieldsCounter").getData();
console.log(oSelectedFields);//(2)
oSelectedFields.counter++;
I have done with a simple button where i retrieve the model data and print it in console.
You could use this code and make it work for you. Else you can add your failing code that needs to be checked further.
I think you would be using the 2-way binding and hence the model is being changed by some action.
Upvotes: 1