Knockout.js映射忽略

Knockout.js mapping ignore

本文关键字:映射 js Knockout      更新时间:2024-05-24

我们正在使用knockout.js和knockout.mapping.js。NET MVC 4。假设我有这样的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"
    } 
}

表单提交失败时(如果服务中出现错误/无效),将使用以前的值重新填充。我们使用$('#BuildingsJSON').val(ko.mapping.toJSON(viewModel.deliveryBuildings))在表单提交中将ViewModel转换为JSON。

在表单重新填充时,我们使用ko.mapping.fromJSON(deliveryBuildings, mapping, viewModel.deliveryBuildings)();解析JSON。mapping目前只是一个空对象(尝试过"忽略",但没有成功)。

我们使用select2字段从列表中选择建筑物的地址(使用ajax)。问题是,fromJSON几乎将每个json属性都填充为可观察的,这是我不需要的。在select2打开时,我们得到一个异常:

未捕获类型错误:对象函数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"

操作return latestValue;}}没有"toUpperCase"方法

我已经调试了ajax调用的类型属性在哪里中断。我认为我们需要将ajax属性从强制转换为可观察属性中排除。

所以,问题是:如何才能将特定对象的特定属性强制转换为observable()?使用映射插件是否足够,是否需要额外的插件,甚至是不可能的?

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

来自文件:

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