Pushing Values Into nested ko.observableArray
我想根据JSON Payload中的项目是否需要供应商,将供应商列表嵌套到从服务器获取的现有JSON Payload中。
我最后想要的是这样的东西:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | { "ProductName":"Product123", "RequiredComponents":"CAP 10% H/Vol", "StockCode":"142111411", "RequiredQtyByBom": 4, "QtyUnassignedInWarehouse": 0, "QtyAllocatedInWarehouse": 40, "PCBReference":"442C", "QtyOnOrder": 26, "Vendors": [], "RequireVendor": false }, { "ProductName":"Product123", "RequiredComponents":"Screws", "StockCode":"Screws", "RequiredQtyByBom": 1, "QtyUnassignedInWarehouse": 0, "QtyAllocatedInWarehouse": 14, "PCBReference":"Screws", "QtyOnOrder": 26, "Vendors": [ {"VendorID":"3", "VendorName":"ABC Supplier", "VendorMOQ": 50000, "VendorItemPrice": 322}, {"VendorID":"4", "VendorName":"DEF Supplier", "VendorMOQ": 4, "VendorItemPrice": 120} ], "RequireVendor": true }, { "ProductName":"Product123", "RequiredComponents":"14141415", "StockCode":"151555231", "RequiredQtyByBom": 1, "QtyUnassignedInWarehouse": 0, "QtyAllocatedInWarehouse": 170, "PCBReference":"1414", "QtyOnOrder": 26, "Vendors": [], "RequireVendor": false } |
我正在考虑使用2 AJAX调用将值推入Observable数组中。
AJAX呼叫1:为产品创建初始有效负载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | MyDataViewModel.SelectedOrderID.subscribe = ko.computed(function () { $.ajax({ url:"/URLToMethod/GetBomStockByProductID", data: { OrderID: ko.toJS(MyDataViewModel.SelectedOrderID) }, type:"GET", contentType:"application/json; charset=utf-8", dataType:"JSON", timeout: 10000, success: function (Result) { for (var i = 0; i < Result.d.length; i++) { element = Result.d[i]; MyDataViewModel.CheckStock.push({ ProductName: element.ProductName, RequiredComponents: element.RequiredComponents, StockCode: element.StockCode, RequiredQtyByBom: element.RequiredQtyByBom, QtyUnassignedInWarehouse: element.QtyUnassignedInWarehouse, QtyAllocatedInWarehouse: element.QtyAllocatedInWarehouse, PCBReference: element.PCBReference, QtyOnOrder: element.QtyOnOrder, Vendors: ko.observableArray(), RequireVendor: ko.computed(function () { if ((element.RequiredQtyByBom * element.QtyOnOrder) > element.QtyAllocatedInWarehouse) { return true } else { return false } }) } ); } }, error: function (xhr, status) { alert(status +" -" + xhr.responseText); } }); }); |
AJAX调用2:将值推送到供应商ko.observableArray()在第一个有效负载中创建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | MyDataViewModel.PurchaseReqHasVendorDetails = ko.computed(function () { var self = MyDataViewModel; for (var i = 0; i < self.CheckStock().length; i++) { if (self.CheckStock()[i].RequirePO()) { $.ajax({ url:"/URLToMethod/GetVendorsByProductName", data: { ProductName: self.CheckStock()[i].ProductName }, type:"GET", contentType:"application/json; charset=utf-8", dataType:"JSON", timeout: 10000, success: function (Result) { for (var i = 0; i < Result.d.length; i++) { element = Result.d[i]; self.CheckStock()[i].Vendors.push({ VendorID: element.VendorID, VendorName: element.VendorName, VendorMOQ: element.VendorMOQ, VendorPrice: element.VendorPrice }); } }, error: function (xhr, status) { alert(status +" -" + xhr.responseText); } }); return true; } else { return false; } } }); |
但这是行不通的。 它创建了初始有效载荷,但没有获得第二个有效载荷,并将其推入我创建的observableArray中。
任何建议将不胜感激
您可以使用映射插件轻松完成此操作。 假设您从服务器检索到稍微不同的JS(或在收到它后对其进行一些修改),如下所示:
1 2 3 | var data = { [ /* Your example data with products and nested vendors here */ ] } |
创建视图模型可以很简单:
1 2 | var viewModel = ko.mapping.fromJS(data); ko.applyBindings(viewModel); |
如果您不希望自动生成视图模型,而是使用自己的ViewModel构造函数,则可以使用映射插件添加其他"映射选项"。 该文档中有很多很好的例子。
这是一个包含您的数据的演示的小提琴。