cancel
Showing results for 
Search instead for 
Did you mean: 

Add new entry to local model

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

kammaje_cis
Active Contributor
0 Kudos

I believe the key here is to use the binding context.

When you add a 'person' to a 'partner', you should have a control to which you have bound the partner. If you get binding context of that control, 'path' from the binding context should give you the row number of the partner.

Former Member
0 Kudos

Hi Krishna,

I was wondering if there was some already built in functionality to add the new person in the right place given the context I'll give a try with the path information taken from the context anyway!

Thanks

Answers (1)

Answers (1)

former_member182650
Contributor
0 Kudos

It could be useful this method to update a customer in model passing binding context and sPath position:

https://sapui5.hana.ondemand.com/sdk/#docs/api/symbols/sap.ui.model.odata.ODataModel.html#setPropert...

For new customer I think you must push new customers to your existing array in context {customers : EXISTING}

Hope this helps you,

Kind regards