knockout.mapping - 将包含嵌套对象的整个对象转换为knockout.observable

时间:2013-09-18 14:46:46

标签: knockout.js knockout-mapping-plugin

我正在使用knockout.mapping将我的javascript对象转换为knockout observable。在这个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)

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

enter image description here

转化后

enter image description here

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

1 个答案:

答案 0 :(得分: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'));
});

正如您所看到的......将这些复杂对象映射到observables实际上是一个简单的插件。一切都被束缚了,你可以把它推到任何一种可编辑的环境中去疯狂......一切都在那里,你可以在XXXViewModel的声明中扩展你的模型,对象的每个实例都会变得更聪明,更聪明

希望这有帮助!