繁体   English   中英

Knockout.js映射忽略

[英]Knockout.js mapping ignore

我们正在使用.NET MVC 4上的knockout.js和knockout.mapping.js。假设我有这样的JSON:

{
    "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 ko.mapping.fromJSON(deliveryBuildings, mapping, viewModel.deliveryBuildings)(); mapping现在只是一个空对象(尝试“忽略”,没有运气)。

我们使用select2字段从列表中选择建筑物的地址(使用ajax)。 事实上,fromJSON几乎将每个json属性填充为可观察的,我不需要。 在select2 open上我们得到一个例外:

未捕获的TypeError:对象函数observable(){if(arguments.length> 0){//写

  // 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; 没有方法'toUpperCase'

我调试了它破坏的地方 - 在ajax调用的类型属性上。 我想我们需要将ajax属性从强制转换为observable。

所以,问题是:怎么看都不施放特定对象的特定属性,以observable() 使用映射插件是否足够,是否需要额外的插件或甚至不可能?

您是否考虑过在映射绑定中使用copy关键字? 这只是将值复制到js属性中,而不是使其成为可观察的。

文档

var mapping = {
    'copy': ["propertyToCopy"]
}
ko.mapping.fromJS(data, mapping, viewModel);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM