Guest User

Untitled

a guest
Nov 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. // Include this mapping to enable pagination via knockout
  2. // 'paginate': {
  3. // create: function(options) {
  4. // return new paginationViewModel(options.data);
  5. // }
  6. // }
  7.  
  8. // TODO reference to "ViewModel" will need to be dynamic
  9.  
  10. var paginationViewModel = function(data) {
  11. this.per_page_limits = ko.observableArray([1, 30, 60, 100, 200, 300]);
  12.  
  13. ko.mapping.fromJS(data, {}, this);
  14.  
  15. var self = this;
  16. self.go_to_prev_page = function() {
  17. if (self.prev_page() && self.prev_page() < self.page()) {
  18. paginate({}, self.prev_page(), self.per_page());
  19. }
  20. }
  21.  
  22. self.go_to_next_page = function() {
  23. if (self.next_page() > self.page()) {
  24. paginate({}, self.next_page(), self.per_page());
  25. }
  26. }
  27. }
  28.  
  29. $('select.per_page:focus').live('change', function() {
  30. paginate({}, 1, $(this).val());
  31. });
  32.  
  33. $("input[name='page']").live('keypress', function(evt) {
  34. if (evt.which == 13 && ViewModel.paginate().total_pages() > 1) {
  35. paginate({}, $(this).val(), ViewModel.paginate().per_page());
  36. }
  37. });
  38.  
  39.  
  40. // This method needs to be created for each till we find a easy solution
  41. // to replicate it for each proper resource
  42. // var paginate = function(attrs, page, per_page) {
  43. // attrs.per_page = per_page;
  44. // attrs.page = page;
  45. // $.getJSON("#{path_to_get}.json", attrs, function(data) {
  46. // ko.mapping.fromJS(data, viewModel, ViewModel);
  47. // });
  48. // }
Add Comment
Please, Sign In to add comment