When fetching data from a web API and pushing it into an observable array, I wanted to make the items in the array observable as well. Unfortunately, I found that I couldn't access the object if I made it observable.
function UpdateViewModel() {
var vm = this;
vm.ProfileInfo = ko.observableArray([]);
vm.GetProfileData = function() {
$.ajax({
type: 'GET',
success: function() {
$.each(data.ProfileList, function (index, value) {
vm.ProfileInfo.push(value);
alert(vm.ProfileInfo()[index].Name) // Success
}
}
});
}
vm.GetProfileData();
}
function UpdateViewModel() {
var vm = this;
vm.ProfileInfo = ko.observableArray([]);
vm.GetProfileData = function() {
$.ajax({
type: 'GET',
success: function() {
$.each(data.ProfileList, function (index, value) {
vm.ProfileInfo.push(ko.observable(value));
alert(vm.ProfileInfo()[index].Name) // Fail. Object does not support property or method 'Name'
}
}
});
}
vm.GetProfileData();
}