I have a ViewModel like this:
function FooViewModel() {
var self = this;
self.prevPage = ko.observable();
self.nextPage = ko.observable();
self.totalItems = ko.observable();
self.totalPages = ko.observable();
$.ajax({
type: 'GET',
url: 'http://localhost:xxxx/api/',
datatype: 'json',
success: function (data) {
//
},
error: function (err){
//
},
complete: function(request){
//Pagination information in the responseheader.
pagingheader = request.getResponseHeader('X-Pagination');
var json = JSON.parse(pagingheader);
self.prevPage(json["PrevPageLink"]);
self.nextPage(json["NextPageLink"]);
self.totalItems(json["TotalCount"]);
self.totalPages(json["TotalPages"]);
console.log(self.totalPages()) // Writes the correct number.
}
});
}
var vm;
$(document).ready(function () {
vm = new FooViewModel();
ko.applyBindings(vm);
// Here I want to access the different properties of the ViewModel.
console.log(typeof vm.totalPages) // writes function
console.log(typeof vm.totalPages()) //writes undefined
console.log(vm.totalPages()); // Writes undefined
});
I've looked at this knockout.js access viewModel in javascript function outside viewModel's scope.
Is there a way I can access the ViewModels properties in document.ready ?