Created
February 6, 2014 21:24
-
-
Save petrjirasek/8852736 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {"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