Check this http://jsfiddle.net/pTEbA/268/
Object.prototype.getName = function() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((this).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
};
function StateViewModel(data){
this.name = ko.observable();
ko.mapping.fromJS(data, mapping, this);
}
function CityViewModel(data) {
this.name = ko.observable();
ko.mapping.fromJS(data, mapping, this);
}
function StreetViewModel(data) {
this.name = ko.observable();
ko.mapping.fromJS(data, mapping, this);
}
var mapping = {
'cities': {
create: function(options) {
return new CityViewModel(options.data);
}
},
'streets': {
create: function(options) {
return new StreetViewModel(options.data);
}
}
}
var data = { state: {name:'SD', cities:[{name:'Sioux Falls',streets:[{number:1},{number:3}]},
{name:'Rapid City',streets:[{number:2},{number:4}]}]}};
var vm = new StateViewModel(data.state)
console.log(vm);
console.log(vm.getName());
console.log(vm.cities());
console.log(vm.cities()[0].getName());
console.log(vm.cities()[0].streets());
console.log(vm.cities()[0].streets()[0].getName());