2017-11-07 10 views
0

나는이 jSON 구조 있습니다. 녹아웃 매핑 플러그인여분 proeprties와 JS에서 녹아웃 매핑

{ 
"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); 
     })()); 
    } 
}, 

}

을이 주 JSON에 "SelectedAddress"를 추가 :

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 
      }); 
     } 
    } 

둘 다 가질 방법을 찾을 수 없습니다

아이디어가 있으십니까?

고맙습니다.

답변

0

알아 냈습니다. 누군가를 위해서. generatint가 "주소"를 매핑 할 때처럼 간단하게 다른 매핑을 추가합니다.

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 
     }); 
    } 
} 

은}

감사합니다!