javascript 在 SAPUI5/OpenUI5 中更新 JSON 模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24097072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating JSON Model in SAPUI5/OpenUI5
提问by devo
I have one json model, model/salesOrder.json,
我有一个 json 模型,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.
在创建的时间Sales Order
,创建具有直接键像一个对象FirstName
,LatName
等等。并在HeaderSet
没有任何键的情况下推送到此数组。
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?
假设我在这个模型中有 3 个对象,我需要更新这个模型中的一个特定对象?我怎样才能做到这一点?
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/3464458and http://scn.sap.com/thread/3386927
这里几乎相同的线程http://scn.sap.com/thread/3464458和http://scn.sap.com/thread/3386927
回答by qmacro
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.
您有一个客户端模型 (JSON),并希望将 HeaderSet 数组的元素替换为来自可编辑表单的对象的更新版本。我们在这里假设您不想直接将双向绑定返回到 JSON 模型,因为例如您想先进行输入验证。
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:
您有一个唯一的键 SalesOrderID,当您发布到事件总线上时,我们可以看到您已经在使用它。让我们假设它存储在 currentSalesOrderID 中,并使用它找到正确的对象以在数组中用新数据替换,在我们将调用的对象中,我们将调用 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);