2

http://jsfiddle.net/VqenG/

希望一些 knockout.js 大师可以对此有所了解

我试图让联系人对象进入联系人可观察数组,该数组本身就是联系人组对象中的可观察数组,但我不明白该怎么做?甚至有可能,还是我以错误的方式接近它?谢谢!

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

2 回答 2

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/

于 2013-07-18T19:05:44.590 回答
0

看看这个小提琴ko.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 也很强大。

我希望它有所帮助。

于 2013-07-18T19:07:53.773 回答