Reputation: 23481
I have one json model, model/salesOrder.json,
{
"HeaderSet" : []
}
The initialization,
var SalesOrderModel = new sap.ui.model.json.JSONModel("model/SalesOrder.json");
sap.ui.getCore().setModel(SalesOrderModel, "SOModel");
On the time of creating Sales Order
, creating one object which have direct keys like FirstName
,LatName
,etc. and pushing to this HeaderSet
array without any keys.
var salesOrderModel = this.getView().getModel("SOModel");
var salesOrderData = salesOrderModel.getData();
salesOrderData.HeaderSet.push(SoObject);
salesOrderModel.setData(salesOrderData);
Suppose I have 3 objects in this model, I need to update a specific object in this model? How can I do that?
Master Page View,
<List id="listMyOrder" growing="true" growingThreshold="10" growingScrollToLoad="true" noDataText="No drafts found" mode="SingleSelectMaster" items="{path : 'SOModel>/HeaderSet' }" select="handleMyOrderSelect">
<ObjectListItem title="{SOModel>PurchaseOrderNumber}">
<attributes>
<ObjectAttribute text="Due Date : {path : 'SOModel>PurchaseOrderDate' }" />
</attributes>
</ObjectListItem>
</List>
Master Page Controller,
handleMyOrderSelect: function (evt) {
this._showDetail(evt.getParameter("listItem"));
},
_showDetail: function (item) {
_salesOrderIDForCart = item.getBindingContext("SOModel").getObject().SalesOrderID
sap.ui.getCore().getEventBus().publish("nav", "to", {
id: "SoDetail",
data: {
source: "MyOrders",
SalesOrderID: item.getBindingContext("SOModel").getObject().SalesOrderID,
context: item.getBindingContext("SOModel")
}
});
}
Now, I can directly bind other details in detail page using this context,
<List showSeparators="None" id="tblSummary">
<InputListItem label="PO Number">
<Label text="{SOModel>PurchaseOrderNumber}" design="Bold"/>
</InputListItem>
<InputListItem label="PO Date">
<Label text="{path : 'SOModel>PurchaseOrderDate'}" design="Bold"/>
</InputListItem>
</List>
In this detail page I have a button which will read this context and populate in an editable form. And here I need to update this specific edited item in the correct object of the model. I tried by adding a unique dynamic key to each object, but at that time my binding will not work.
Almost same thread here http://scn.sap.com/thread/3464458 and http://scn.sap.com/thread/3386927
Upvotes: 0
Views: 20381
Reputation: 3105
You have a client model (JSON) and want to replace an element of the HeaderSet array with an updated version from the object that comes from your editable form. We're assuming here that you're not wanting to directly have two-way binding back to the JSON model, because you want to do input validation first, for example.
You have a unique key SalesOrderID that we can see you already using when you publish onto the event bus. Let's assume this is stored in currentSalesOrderID and use this to find the right object to replace in the array with the new data, in an object we'll call updatedOrder:
// Get the data from the model
var salesOrderData = salesOrderModel.getData();
// Find the index of the object via the SalesOrderID
var index = salesOrderData.HeaderSet
.map(function(order) { return order.SalesOrderID; })
.indexOf(currentSalesOrderID);
// Replace the order in the array
salesOrderData.HeaderSet.splice(index, 1, updatedOrder);
Upvotes: 2