Having a go at playing with Knockout and I'm having problems with pushing to an observable array. My code below fetches some json data from the server on a button click, it's returning an array of objects. The first console.log consoles out fine, I have my array of objects just fine and dandy.
I can't seem to push each object onto my observable array, however. The console shows me empty arrays. I've tried several variations, but feel like I'm just missing something simple, yet I'm having a hard time tracking it down.
What I'm trying to do is load up some data from the server, put it in an observable array that I can bind to a template and do something like a foreach on to output the contents of the array.
$(function() {
$('#load').click(function() {
$.getJSON('/PreferredDrugList/service/preferredDrugs/y', function(data) {
$(data.preferredDrugs).each(function(index, obj) {
console.log(obj);
$('#result').append('<p>' + (++index) + ') ' + obj.drugName + ' : ' + obj.dosageFormDesc + '</p>');
myViewModel.drugList.push(obj);
console.log(myViewModel.drugList);
});
});//end getJSON
});//end load
});//end ondomready
var myViewModel = {
drugList: ko.observableArray() //list of drug names
};
ko.applyBindings(myViewModel);