Skip to Content
0

SAPUI5 Array update issue

Mar 13, 2017 at 03:10 PM

64

avatar image

Hi,

I've a global Model for entire application. And a local model for one view. Array is in the global model.

1. Created view elements and binding with local model created on view

2. reading values from local model and trying to push to global model

Issue: very first element is pushing fine. But, when I try to push the second item to the global model, the first element which is already there in the model is updated with the new values entered on the screen. Am wondering, there is not binding though to global Model to get set by the framework.

Thought, to create local array in view controller itself and tried to push. Even, here also same thing is happening. when I read the model(model.getProperty) itself, the new values are directly getting updated to the model array.

var vModel = this.getView().getModel();
// Local Model
var vData = vModel.getProperty("/EduData");

var ownerComp = this.getOwnerComponent();
// Global Model 
var empModel = ownerComp.getModel("EmpData");
// Global Model Array
var EduDetails = empModel.getProperty("/Employees/EducationalInfo");

var eduModel = ownerComp.getModel("EduData"); // Local Model 
// Reading user input values
var modelData = eduModel.getProperty("/"); 

// Pushing to local Model
vData.push(modelData);
vModel.refresh();
// Pushing to Global Model 
EduDetails.push(modelData);
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Jun Wu Mar 13, 2017 at 04:29 PM
0

// Reading userinputvalues

var modelData = eduModel.getProperty("/"); // Pushing tolocal Model
vData.push(JSON.parse(JSON.stringfy(modelData)));
Show 1 Share
10 |10000 characters needed characters left characters exceeded

do the same for other push if needed.

0
Ulrich Block
Mar 13, 2017 at 03:57 PM
0

What is the goal of this model manipulation? Would it be possible to use named models instead? At the setModel function you could use sth like:

oControl.setModel(oModel, "yourAlias");

At the data binding part use sth like:

"yourAlias>EduData/"
Share
10 |10000 characters needed characters left characters exceeded
CHENGALARAYULU DAMALACHERUVU Mar 13, 2017 at 04:08 PM
0

Hi Ulrich, Thanks for coming back, binding and alias names everything is perfect. There is no error as such. But, the goal is, I want to read data from UI and push it to an array of the model. I will try to explain you bit more what is happening at the moment.

array A1 = [ { A B C }

{ X Y Z} ]

above is the data initially there in the model. Now, user is trying to add few more inputs and am reading them and adding using PUSH method.

{L M N} - this I've read from UI and pushed to A1 and L M N are added at the bottom as array element 2. first time it is fine. again user tried to add one more

{O P Q} - here onwards the issue starts. L M N will also be replaced by O P Q. All further elements will be replaced by the latest element values.

Now, the array A1 = [ { A B C }

{ X Y Z}

{O P Q}

{O P Q}] - please suggest.

Share
10 |10000 characters needed characters left characters exceeded