2013-07-18 112 views
2

http://jsfiddle.net/VqenG/多對象淘汰賽映射

希望能有些knockout.js大師可以擺脫這個

我試圖讓聯繫對象進入接觸觀察到的陣列,它本身就是內可觀察到的陣列的一些光ContactGroup對象,但我不明白如何做到這一點?這是可能的,還是我以錯誤的方式接近它?謝謝!

var json = 
    {"contactGroups" : [ 
    { 
      "name" : "Contact Group", 
      "contact" : [ 
        { 
          "name" : "aaaa", 
          "email" : "", 
          "telephone" : "", 
          "mobile" : "", 
          "mail_group" : "", 
          "comment" : "" 
        }, 
        { 
          "name" : "bbbb", 
          "email" : "", 
          "telephone" : "", 
          "mobile" : "", 
          "mail_group" : "", 
          "comment" : "" 
        }, 
        { 
          "name" : "cccc", 
          "email" : "", 
          "telephone" : "", 
          "mobile" : "", 
          "mail_group" : "", 
          "comment" : "" 
        } 
      ] 
    } 
]} 



function TechnicalViewModel(){ 
    self = this; 

    var ContactGroups = ko.utils.arrayMap(json.contactGroups, function(item) { 
     var group = new ContactGroup(item); 
     var contacts = ko.utils.arrayMap(item.contact, function(item) { 
     return new Contact(item) 
     }); 
     group.contact(contacts) 
     return group; 
    }) 
    self.contactGroups(ContactGroups) 

    function ContactGroup(data){ 
     var self = this; 

     self.name = ko.observable(data.name); 
     self.contact = ko.observableArray([]); 

     function Contact(data){ 
      this.name = ko.observable(data.name); 
      this.email = ko.observable(data.email); 
      this.telephone = ko.observable(data.telephone); 
      this.mobile = ko.observable(data.mobile); 
      this.mail_group = ko.observable(data.mail_group); 
      this.comment = ko.observable(data.comment); 
     }  
    } 
} 

TechnicalView = new TechnicalViewModel 
ko.applyBindings(TechnicalView); 

回答

1

你必須將你的Contact功能ContactGroup功能之外。它的作用域限於ContactGroup函數內,並且不能被ko.utils.arrayMap函數看到。將它移動到外面會將範圍擴大到整個TechnicalViewModel

function TechnicalViewModel(jsonData){ 
    self = this; 

    var ContactGroups = ko.utils.arrayMap(jsonData.contactGroups, function(item) { 
     var group = new ContactGroup(item); 
     var contacts = ko.utils.arrayMap(item.contact, function(item) { 
      return new Contact(item) 
     }); 
     group.contact(contacts) 
     return group; 
    }); 

    self.contactGroups = ko.observableArray(ContactGroups); 

    function Contact(data) { 
     this.name = ko.observable(data.name); 
     this.email = ko.observable(data.email); 
     this.telephone = ko.observable(data.telephone); 
     this.mobile = ko.observable(data.mobile); 
     this.mail_group = ko.observable(data.mail_group); 
     this.comment = ko.observable(data.comment); 
    } 

    function ContactGroup(data){ 
     var self = this; 

     self.name = ko.observable(data.name); 
     self.contact = ko.observableArray([]); 
    } 
} 

var technicalVM = new TechnicalViewModel(json); 
ko.applyBindings(technicalVM); 

http://jsfiddle.net/VqenG/3/

+0

非常感謝!需要刷上我的範圍! – Wellso

0

看一看this fiddleko.mapping.fromJS

ko.mapping.formJS允許這樣做:

在一行
function Contact(data){ 
    this.name = ko.observable(data.name); 
    this.email = ko.observable(data.email); 
    this.telephone = ko.observable(data.telephone); 
    this.mobile = ko.observable(data.mobile); 
    this.mail_group = ko.observable(data.mail_group); 
    this.comment = ko.observable(data.comment); 
}  

function Contact(data){ 
    ko.mapping.fromJS(data, {}, this); 
} 

ko.mapping.fromJS也是強大的,當你要根據新的刷新視圖模型數據來自服務器。

我希望它有幫助。