希望一些 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);