在我的应用程序中,我有一个事件用于向我的可观察数组添加一个新的嵌套对象。在我的代码中,我试图这样做:
- 将我的可观察数组更改为标准 JS 对象。让我们称之为 objAgency。
- 创建一个具有属性的新对象。我将称之为 objContact。
- 我使用代码中的 ID 拼接来自 objAgency 的旧数据。
- 我在它的位置添加了 objContact。
- 我将 objAgency 转换回 observable。
我将 objAgency 映射到我的绑定,但这给了我以下错误:
Unhandled exception at line 1936, column 17 in http://localhost:13762/scripts/knockout-2.2.1.debug.js 0x800a139e - JavaScript runtime error: Unable to parse bindings. Message: ReferenceError: 'router' is undefined; Bindings value: css: { active: router.isNavigating }
这是我的 agent.js 文件
define(['services/datacontext'], function (dataContext) {
var initialized = false;
var agency;
agency = ko.observableArray([]);
brands = ko.observableArray([]);
var vm = { // This is my view model, my functions are bound to it.
//These are wired up to my agency view
activate: activate,
agency: agency,
brands: brands,
title: 'agency',
refresh: refresh, // call refresh function which calls get Agencies
save: save,
cacheForm: cacheForm,
addOffice: addOffice,
addBrand : addBrand,
removeBrand: removeBrand,
addContact: addContact,
removeContact: removeContact
};
return vm;
function activate() {
vm.agency;
if (initialized) {
return;
}
initialized = false;
refresh();
}
function refresh() {
dataContext.getAgency(agency);
dataContext.getBrands(brands);
}
function addBrand() {
brands.unshift({
brandName : ""
});
// Change td css to editable textbox
jQuery("#tblBrand td:first input").removeAttr('readonly');
}
function removeBrand(brand) {
brands.remove(brand);
}
这是我的代码中断的 addContact 函数
function addContact(office) { // Passing in object array of agency. We no it contains correct office and agency ID
// Convert agency to object
objAgency = ko.toJS(agency);
// Get ID of office I am changing
var officeID = office.officeID._latestValue;
// Convert from observable to vanilla object
objOffice = ko.toJS(office);
// Fill new object with empty strings and related data
var contact = {
agencyID: office.agencyID._latestValue,
emailAddress: "",
firstName: "",
jobName: "",
office: "",
OfficeID: office.officeID._latestValue,
personID: "",
surName: "",
title: ""
}
// Unshift new object to front of object. It will be first row in table.
objOffice.contacts.unshift(contact);
// Convert back into observable
//obsOffice = ko.observableArray([ko.mapping.fromJS(objOffice)]);
// Splice where office ID match
for (i in objAgency[0].offices) {
if (!isNaN(i)) {
if (objAgency[0].offices[i].officeID === officeID) {
objAgency[0].offices.splice(i, 1); // At i remove one object
}
else {
}
}
}
objAgency[0].offices.unshift(objOffice);
agency = ko.observableArray([ko.mapping.fromJS(objAgency[0])]);
vm.agency = agency;
ko.applyBindings(objAgency);
}
function removeContact(contact) {
for (i in agency._latestValue[0].offices._latestValue) {
if (isNaN(i)) { // Escape if NaN, otherwise use index valI ha
}
else {
for (ii in agency._latestValue[0].offices._latestValue[i].contacts._latestValue) {
agency._latestValue[0].offices._latestValue[i].contacts.remove(contact);
}
}
}
}
});
我不明白为什么ko.applyBindings(objAgency); 不起作用,因为除了删除一个嵌套对象并在其位置添加一个新对象之外,该对象没有更改。