Skip to Content
0
Former Member
Dec 18, 2013 at 05:05 PM

Add new entry to local model

44 Views

Hello everyone,

I have a local model, like this one:

{

"partners" : [

{

"name" : "Coca Cola",

"icon" : "/img/cocacola.png",

"people" : [

{

"name" : "Jonh",

"surname" : "Wayne",

"role" : "Developer"

},

{

"name" : "Matt",

"surname" : "Damon",

"role" : "Manager"

},

{

"name" : "Super",

"surname" : "Man",

"role" : "Hero"

},

... //other workers

]

},

... //other partners

]

}

I have created a view that shows the partner's people list. The view sees and listens to a subportion of the model, at this path:

/partners/n/people

where n is the nth partner selected. Long story short, using data binding through 2 views, I display the list of partners, then when the user clicks on a partner I show the list of peoples of that partner (path /partners/n/people), and then when the user clicks on a person, I show the person details (path /partners/n/people/n).

What I want to do now is create a view to add a new person to a partner's people list. On the "people's view" I added a popup that the user can use to input all the data needed to create the new person:

When the user clicks on the save button, I collect all the user inputs and create an object like this:

...

var name = sap.ui.getCore().byId("inputName").getValue();

var surname= sap.ui.getCore().byId("inputSurname").getValue();

var role= sap.ui.getCore().byId("inputRole").getValue();


var newPerson = {

name : name,

surname : surname,

role : role

}

How can I now save this new object inside the right partner's people array? I did something like this in the past, but with a 1 level array, like this:

...

var newCustomer = {name : name, surname : surname, city : city, ...}

var oModel = sap.ui.getCore().getModel(); //--- get the model

var oData = oModel.getData(); //--- get the model odata

var oDelta = {customers : []}; //--- create a delta obj

oDelta.customers[oData.customers.length] = newCustomer; //--- add the newCustomer @nth position

oModel.setData(oDelta, true); //--- add new data!

How can I place my newPerson in the right place? Do I have to keep track of the current partner, and then loop through all the partners to find the matching one, and then append the newPerson to the people array?

I hope I was clear enough, thanks in advance