【问题标题】:knockout mapping from JS with extra proeprties来自具有额外属性的 JS 的淘汰赛映射
【发布时间】:2018-04-19 16:44:55
【问题描述】:

我有这个 JSON 结构。

{
"customer": {
    "idcustomer": 2,
    "name": "test_2",
    "vat": "test_vat_2",
    "obs": "obs_2",
    "deleted": 0
},
"addresses": [
    {
        "idaddress": 9,
        "street": "street_2_9",
        "number": "number_2_9",
        "country": "country_2_9",
        "default": true,
        "label": "labe_2_9",
        "deleted": 0
    },
    {
        "idaddress": 10,
        "street": "1",
        "number": "number_2_9",
        "country": "country_2_10",
        "default": false,
        "label": "label_2_10",
        "deleted": 0
    }
],
"contacts": []

}

使用淘汰赛映射插件,我能够生成淘汰赛可观察对象。但是,当尝试使用映射参数向对象添加额外属性时,我发现了一些问题。目标是将“SelectedAddress”添加到主对象,并在每个地址中添加一个“defaultLabel”观察值。

目前我有这个映射结构来将属性添加到地址子项:

var mapping = {

'addresses': {
    create: function (options) {
        return (new (function () {
            this.defaultLabel= ko.computed(function () {
                return (this.default() == 0) ? "" : this.label();
                }, this);
            ko.mapping.fromJS(options.data, {}, this);
        })());
    }
},

}

并将“SelectedAddress”添加到主 JSON:

create: function (options) {
        return new function () {

            var model = ko.mapping.fromJS(options.data, {}, this);
            // Direccion
            model.direccionSeleccionada = ko.observable();
            model.getDireccion = ko.computed({
                read: function() {
                    if (model.direccionSeleccionada() != null) {
                        return model.direccionSeleccionada();
                    } else {
                        return [{
                                idaddress: -1,
                                street : '',
                                number: '',
                                country: '',
                                default: '',
                                label: '',
                                deleted: '',
                        }];
                    }
                },
                write: function(value) {
                    self.direccionSeleccionada(value);
                },
                owner: self
            });
        }
    }

我找不到同时拥有它们的方法

想法?

谢谢

【问题讨论】:

    标签: javascript json knockout.js knockout-mapping-plugin


    【解决方案1】:

    我想通了。只为某人;就像生成“地址”的映射一样简单,为它添加另一个映射。

    var mapping = {
    create: function (options) {
        return new function () {
            var model = ko.mapping.fromJS(options.data, {
                'adresses': {
                    create: function (options) {
                        return (new(function () {
                            this.labelDefault= ko.computed(function () {
                                return (this.default() == 0) ? "" : this.label();
                            }, this);
                            ko.mapping.fromJS(options.data, {}, this);
                        })( /* call the ctor here */ ));
                    }
                },
            }, this);
    
            model.direccionSeleccionada = ko.observable();
            model.getDireccion = ko.computed({
                read: function () {
                    if (model.selectedAddress() != null) {
                        return model.selectedAddress();
                    } else {
                        return [{
                            idaddress: -1,
                            street: '',
                            number: '',
                            country: '',
                            default: '',
                            label: '',
                            deleted: ''
                        }];
                    }
                },
                write: function (value) {
                    self.selectedAddress(value);
                },
                owner: self
            });
        }
    }
    

    }

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 2012-06-08
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      相关资源
      最近更新 更多