我有一些代码可以添加到敲除可观察数组中,我正在将可观察数组转换为对象,取消移动新对象,然后将该对象映射回视图模型。这有效,但似乎很慢。大约需要 2-5 秒或更长时间。
function addContact(office) { // Passing in object array of agency. We no it contains correct office and agency ID
// Assign observable data to new variable then remove old
// variable from mapping
var objAgency = ko.toJS(agency);
vm.agency.removeAll();
// Fill new object with empty strings and related data
var objContact = {
agencyID: office.agencyID._latestValue,
emailAddress: "",
firstName: "",
jobName: "",
office: "",
OfficeID: office.officeID._latestValue,
personID: "",
surName: "",
title: ""
}
// unshift where office ID match
for (i in objAgency[0].offices) {
if (!isNaN(i)) {
if (objAgency[0].offices[i].officeID === objContact.OfficeID) {
objAgency[0].offices[i].contacts.unshift(objContact); // At i remove one object
}
else {
}
}
}
vm.agency([ko.mapping.fromJS(objAgency[0])]);
}
我尝试添加到我的可观察对象而不是执行转换过程,但出现此错误:
Unhandled exception at line 9423, column 13 in http://localhost:13762/scripts/breeze.debug.js
0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'getProperty'
这是导致错误的代码
for (i in agency._latestValue[0].offices._latestValue) {
if (!isNaN(i)) {
if (agency._latestValue[0].offices._latestValue[i].officeID = objContact.OfficeID) {
agency._latestValue[0].offices._latestValue[i].contacts._latestValue.unshift([ko.mapping.fromJS(objContact)]);
}
}
}
请参阅我的屏幕截图以了解代理机构的外观:
添加到这个可观察数组的正确方法是什么?据我了解,最新价值是一种跟踪变化的机制,所以我不应该篡改它?