I'm attempting to send some data to a Knockout observable array. Despite receiving data from my AJAX call without any errors during script execution, I find that the userNames
array remains empty when accessed. What could be causing this issue?
UserHandler = (function () {
var userName = function (data) {
this.Name = data;
};
var userNames = ko.observableArray([]);
var fetchUserNames = function () {
userNames([]);
$.ajax({
url: "Users/All",
dataType: "json",
success: function (data) {
ko.utils.arrayPushAll(userNames, $.map(data, function (entry) {
return new userName(entry);
}));
}
});
};;
return {
UserNames: userNames,
GetUserNames: fetchUserNames,
};
});
console.log(UserHandler().UserNames());
--> []