I have this model :
var child = function(c){
var self = this;
self.id = ko.observable(c.id);
self.first_name = ko.observable(c.first_name);
self.last_name = ko.observable(c.last_name);
self.gender_id = ko.observable(c.gender_id);
self.birthday= ko.observable(c.birthday);
self.family_id = ko.observable(c.family_id);
}
And i have a viewmodel defined like this:
var childrenViewModel = function(initialData){
var self = this;
//
self.children = ko.observableArray(initialData);
//
self.removeChild = function(){};
};
I am fetching initially data in this way:
$.ajax({
type: 'GET',
url: getChildrenListUrl,
contentType: 'application/json'
})
.done(function (data) {
vmChildren =new childrenViewModel(JSON.parse(data));
ko.applyBindings(vmChildren,document.getElementById('portlet-children'));
Metronic.unblockUI('#portlet-children');
});
I now want to make the contents of the children observablearray, observables themselves by using mapping plugin and i am having difficulties in doing so...
Why do you need ko.mapping
, dont you just want to map initialData
to be instances of child
?
self.children = ko.observableArray(initialData.map(function(e){
return new child(e);
}));
See Array.map