我们正在与.NET的MVC 4 knockout.js和knockout.mapping.js工作比方说,我有这样的JSON:Knockout.js映射忽略
{
"deliveryPointType": "0",
"deliveryPointTypes": [
{
"id": 0,
"text": "Pridėti rankiniu būdu"
},
{
"id": 1,
"text": "Siųsti visiems regiono objektams"
}
],
"showRegionSelection": false,
"showDeliveryPointSelection": true,
"regionId": "",
"userHasRegions": "False",
"propertyNames": {
"deliveryPointTypeName": "Pridėti rankiniu būdu"
},
"initialMaterials": [
{
"quantity": 0,
"materialTypeId": "",
"propertyNames": {},
"validMaterial": true,
"showMaterialError": false,
"materialTypeAjax": {
"quietMillis": 300,
"cache": false,
"dataType": "json",
"type": "GET",
"url": "/lt-LT/Material/MaterialTypeNameLookup"
}
}
],
"deliveryBuildings": [
{
"clientId": "1",
"buildingId": "1",
"regionId": "",
"newBuilding": false,
"validClient": true,
"validBuilding": true,
"validRegion": true,
"showClientError": false,
"showBuildingError": false,
"showRegionError": false,
"propertyNames": {
"clientName": "klientas",
"buildingName": "ASD project, Antagynės gatvė, Kaunas, Lietuvos Respublika"
},
"clientAjax": {
"quietMillis": 300,
"cache": false,
"dataType": "json",
"type": "GET",
"url": "/lt-LT/Task/PayerLookup"
},
"buildingAjax": {
"quietMillis": 300,
"cache": false,
"dataType": "json",
"type": "GET",
"url": "/lt-LT/Object/GetClientAddressListByQuery"
},
"regionAjax": {
"quietMillis": 300,
"cache": false,
"dataType": "json",
"type": "GET",
"url": "/lt-LT/Object/RegionNameLookup"
}
}
],
"hasNewBuildings": false,
"showBuildingValidation": false,
"showMinimumBuildingRequiredValidation": false,
"showMaterialValidation": false,
"validRegion": true,
"showRegionError": false,
"regionAjax": {
"quietMillis": 300,
"cache": false,
"dataType": "json",
"type": "GET",
"url": "/lt-LT/Object/RegionNameLookup"
}
}
在表单提交失败(如果出现在服务中错误/无效)使用之前的值重新填充。我们将ViewModel转换为JSON,表单用$('#BuildingsJSON').val(ko.mapping.toJSON(viewModel.deliveryBuildings))
提交。
在窗体重新填充上,我们使用ko.mapping.fromJSON(deliveryBuildings, mapping, viewModel.deliveryBuildings)();
解析JSON mapping
现在只是一个空对象(尝试“忽略”而没有运气)。
我们使用select2字段从列表中选择建筑物的地址(使用ajax)。事情是,从JSON填充几乎每个JSON属性作为可观察的,我不需要。在选择2开,我们得到一个异常:
Uncaught TypeError: Object function observable() { if (arguments.length > 0) { // Write
// Ignore writes if the value hasn't changed if (!observable['equalityComparer'] || !observable['equalityComparer'](_latestValue, arguments[0])) { observable.valueWillMutate(); _latestValue = arguments[0]; if (DEBUG) observable._latestValue = _latestValue; observable.valueHasMutated(); } return this; // Permits chained assignments } else { // Read ko.dependencyDetection.registerDependency(observable); // The caller only needs to be notified of changes if they did a "read"
operation return _latestValue; } } has no method 'toUpperCase'
我已经调试它打破 - 在Ajax调用的类型属性。我认为我们需要将ajax属性从cast转换为observable。
所以,问题是:怎能不施放特定对象的特定属性来observable()
?使用映射插件是否够用,是否需要额外的插件或者甚至不可能?
谢谢,我会尽力的。而且 - 这是可能的“复制”整个对象? – YOhan
嗯,它仍然将属性转换为可观察的。 – YOhan
你可以告诉我你已经使用过的代码,因为我已经使用了Copy并且它工作正常。 –