3

我正在使用 knockout.mapping 将我的 javascript 对象转换为可观察到的淘汰赛。在这个 javascript 对象内部是一个嵌套对象,这个嵌套对象正在被转换,但它位于错误的位置。因此,当我的视图尝试绑定数据时它不能,因为映射已将嵌套对象移动到根。

JSON数据

[
    {
        "brandName": "Knockando Single Malt Whisky",
        "categoryDescription": "Alcohol - Whisky & Bourbon",
        "dateAmended": "2008-01-28T10:05:00.000Z",
        "defaultSearchName1": "KnockandoSingleMaltWhisky",
        "defaultSearchName2": "Knockando Single Malt Whisky",
        "advertiser": {
            "activities": "Motor dealers",
            "additionalInfo": "",
            "advertiserName": "Parks of Hamilton Ltd",
            "alfRank": 3311,
            "cinemaRank": 286,
            "directMailRank": 1291,
            "internetRank": 1988,
            "notes": "Trade as Parks Motor Group.\nAlternative email: coachhire@park.uk.com, admin@park-uk.com, Info@park.uk.com.",
            "numberOfEmployees": 1000,
            "outdoorRank": 1426,
            "pressRank": 5159,
            "radioRank": 675,
            "tVRank": 1810
        }
    },
    {
        "brandName": "Tesco Petrol/Diesel Fuel",
        "categoryDescription": "Petrol & Service Stations",
        "dateAmended": "2013-07-10T15:17:00.000Z",
        "defaultSearchName1": "TescoPetrol/DieselFuel",
        "defaultSearchName2": "Tesco Petrol/Diesel Fuel",
        "advertiser": {
            "activities": "Furnitures",
            "additionalInfo": "",
            "advertiserName": "Pine & Things Ltd",
            "alfRank": 4570,
            "cinemaRank": 286,
            "directMailRank": 1291,
            "internetRank": 1988,
            "notes": "Trade as P&T.\nAlternative email: info@pineandthings.co.uk.",
            "numberOfEmployees": 25,
            "outdoorRank": 1426,
            "pressRank": 2851,
            "radioRank": 2449,
            "tVRank": 1810
        }
    },
    {
        "brandName": "Tesco Electrical Products",
        "categoryDescription": "Supermarkets & Multiple Retailers",
        "dateAmended": "2013-07-10T15:52:00.000Z",
        "defaultSearchName1": "TescoElectricalProducts",
        "defaultSearchName2": "Tesco Electrical Products",
        "advertiser": {
            "activities": "Bridalwear",
            "additionalInfo": "",
            "advertiserName": "Phil Collins Bridal Collection Ltd",
            "alfRank": 4094,
            "cinemaRank": 286,
            "directMailRank": 1291,
            "internetRank": 1988,
            "notes": "Alternative email: enquiries@philcollinsbridal.co.uk, pcbridal@aol.com.",
            "numberOfEmployees": 30,
            "outdoorRank": 1426,
            "pressRank": 2487,
            "radioRank": 2449,
            "tVRank": 1810
        }
    },
    {
        "brandName": "Tesco Bank Insurance Ranges",
        "categoryDescription": "Insurance Ranges",
        "dateAmended": "2012-01-05T12:30:00.000Z",
        "defaultSearchName1": "TescoBankInsuranceRanges",
        "defaultSearchName2": "Tesco Bank Insurance Ranges",
        "advertiser": {
            "activities": "Nutition supplements",
            "additionalInfo": "",
            "advertiserName": "PhD Nutrition Ltd",
            "alfRank": 4107,
            "cinemaRank": 286,
            "directMailRank": 1291,
            "internetRank": 1988,
            "notes": "Alternative email: info@phd-supplements.com.\nThere is no alternative head office landline number.",
            "numberOfEmployees": 30,
            "outdoorRank": 1426,
            "pressRank": 2500,
            "radioRank": 2449,
            "tVRank": 1810
        }
    },
    {
        "brandName": "Tesco Clubcard",
        "categoryDescription": "Loyalty & Incentive Schemes",
        "dateAmended": "2008-03-26T14:05:00.000Z",
        "defaultSearchName1": "TescoClubcard",
        "defaultSearchName2": "Tesco Clubcard",
        "advertiser": {
            "activities": "",
            "additionalInfo": "",
            "advertiserName": "British Regional Airlines Group PLC",
            "alfRank": 6031,
            "cinemaRank": 286,
            "directMailRank": 1291,
            "internetRank": 1988,
            "notes": "",
            "numberOfEmployees": 0,
            "outdoorRank": 1426,
            "pressRank": 5159,
            "radioRank": 2449,
            "tVRank": 1810
        }
    },
    {
        "brandName": "Royal Mail Corporate",
        "categoryDescription": "Public Services - Corporate",
        "dateAmended": "2010-06-04T11:45:00.000Z",
        "defaultSearchName1": "RoyalMailCorporate",
        "defaultSearchName2": "Royal Mail Corporate",
        "advertiser": {
            "activities": "",
            "additionalInfo": "",
            "advertiserName": "Yorkshire Power Group Ltd",
            "alfRank": 6031,
            "cinemaRank": 286,
            "directMailRank": 1291,
            "internetRank": 1988,
            "notes": "",
            "numberOfEmployees": 0,
            "outdoorRank": 1426,
            "pressRank": 5159,
            "radioRank": 2449,
            "tVRank": 1810
        }
    },
    {
        "brandName": "E.ON Surf & Save ISP",
        "categoryDescription": "Telecom - Internet Service Providers",
        "dateAmended": "2007-11-23T12:25:00.000Z",
        "defaultSearchName1": "E.ONSurf&SaveISP",
        "defaultSearchName2": "E.ON Surf & Save ISP",
        "advertiser": {
            "activities": "",
            "additionalInfo": "",
            "advertiserName": "Stagecoach Ltd",
            "alfRank": 6031,
            "cinemaRank": 286,
            "directMailRank": 1291,
            "internetRank": 1988,
            "notes": "",
            "numberOfEmployees": 0,
            "outdoorRank": 1426,
            "pressRank": 5159,
            "radioRank": 2449,
            "tVRank": 1810
        }
    }
]

Javascript

 // Get JSON from local storage and convert to js object
 var objBrands = new Object(ko.mapping.fromJSON(localStorage.getItem('Brands')));

 // Convert from object to observable
 brands = ko.mapping.fromJS(objBrands)

我的对象在转换为可观察之前的数据

在此处输入图像描述

转换后

在此处输入图像描述

在屏幕截图二中 - 广告客户在错误的位置。我希望它在 [0] 内,但 ko.mapping 解析了我的对象并将其放在那里。

4

1 回答 1

8

这是你今天的香蕉。

您可以做几件事,但我将向您指出使用 ko.mapping 插件的方向。这是我以您的情况为例拟定的小提琴。

http://jsfiddle.net/beauxjames/ps75V/

首先你会看到你的数据,我确实添加了一个小的 json 对象值来启动,并将它加载到一个局部变量::

var rawData = { "brand": [
{...} ]};

接下来,我们有 jQuery、knockout.js 和 knockout.mapping.js。

然后,注意函数类型 ::

var BrandsViewModel = function(data) {
    var self = this;
    ko.mapping.fromJS(data, brandMapping, self);
}

var AdvertiserViewModel = function(data) {
    var self = this;
    ko.mapping.fromJS(data, {}, self);
}

现在,每个里面都有一个 ko.mapping.fromJS() 的声明...这里有详细的解释 :: http://knockoutjs.com/documentation/plugins-mapping.html

然后映射本身::

var brandMapping = {
    'brands': {
        create: function (options) {
            if (options.data != null) return new BrandsViewModel(options.data);
        }
    },
    'advertiser': {
        create: function (options) {
            if (options.data != null) return new AdvertiserViewModel(options.data);
        }
    }
}

然后......业务结束::

$(document).ready(function() {
    var viewModel = new BrandsViewModel(rawData);
    ko.applyBindings(viewModel, document.getElementById('mylist'));
});

如您所见……使用此插件将这些复杂的对象映射到可观察对象确实是一件轻而易举的事。一切都捆绑在一起,您可以将其推送到任何类型的可编辑环境并发疯……一切都在那里,您可以直接从 XXXViewModel 的声明中扩展您的模型,并且对象的每个实例都会变得越来越智能.

希望这有帮助!

于 2013-09-18T19:58:48.237 回答