javascript - Multi object knockout mapping -
hoping knockout.js guru can shed light on this
i trying make contact objects go contact observable array, observable array within contactgroup object, dont understand how this? possible, or approaching wrong way? thanks!
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);
you have move contact function outside of contactgroup function. it's scope limited within contactgroup function , not able seen ko.utils.arraymap function. moving outside increases scope entire 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);
Comments
Post a Comment