Skip to content

Instantly share code, notes, and snippets.

@petrjirasek
Created February 6, 2014 21:24
Show Gist options
  • Select an option

  • Save petrjirasek/8852736 to your computer and use it in GitHub Desktop.

Select an option

Save petrjirasek/8852736 to your computer and use it in GitHub Desktop.
var ProductsViewModel = function() {
var self = this;
self.productsURI = ...;
self.products = ko.observableArray();
self.limit = ko.observable(1);
self.currentPage = ko.observable(1);
self.pageCount = ko.observable(2);
self.goPreviousPage = function() {
self.load(self.currentPage() + 1, self.limit);
}
self.goNextPage = function() {
self.load(self.currentPage() + 1, self.limit);
}
self.load = function(currentPage, limit) {
$.ajax({
dataType: "json",
url: self.productsURI,
data: {sortName: self.sortName, page: currentPage, limit: limit},
success: function(data) {
self.currentPage(data.currentPage);
self.pageCount(data.pageCount);
self.limit(data.limit);
self.products.removeAll();
$.each(data.products, function(key, value) {
self.products.push({
name: ko.observable(value.name)
});
});
}
});
}
}
$(document).ready(function () {
var productsModel = new ProductsViewModel();
ko.applyBindings(productsModel);
productsModel.load(1, 1);
});
{"products":[{"name":"Product 1","ean":"123"},{"name":"Product 2","ean":"123"}],"currentUrl":"http:\/\/localhost:8000\/product\/list\/","currentPage":1,"pageCount":2,"limit":1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment