PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/collections/BaseCollection.js

https://github.com/bozz/cashflow
JavaScript | 78 lines | 50 code | 15 blank | 13 comment | 0 complexity | 991278140e8451b572121b5790cbee4c MD5 | raw file
  1. // BaseCollection module
  2. define(function(require) {
  3. var BaseCollection = Backbone.Paginator.requestPager.extend({
  4. defaultOrder: 'name ASC',
  5. initialize: function(config) {
  6. this.initCustomApiParams();
  7. },
  8. // overwrite this function to set custom parameters
  9. initCustomApiParams: function() {
  10. // this.server_api.custom_param = function() { return this.customParam; }
  11. },
  12. // custom method for triggering remote filtering by search query
  13. applyFilter: function(query) {
  14. this.server_api.q = query;
  15. this.pager();
  16. },
  17. paginator_core: {
  18. type: 'GET',
  19. dataType: 'json',
  20. url: function() {
  21. return this.url();
  22. }
  23. },
  24. paginator_ui: {
  25. firstPage: 1,
  26. currentPage: 1,
  27. perPage: 20,
  28. totalPages: 10
  29. },
  30. server_api: {
  31. // the query field in the request
  32. q: '',
  33. // number of items to return per request/page
  34. per: function() { return this.perPage },
  35. page: function() { return this.currentPage },
  36. // how many results the request should skip ahead to
  37. // customize as needed. For the Netflix API, skipping ahead based on
  38. // page * number of results per page was necessary.
  39. // '$skip': function() { return this.currentPage * this.perPage },
  40. // field to sort by
  41. order: function() { return this.defaultOrder; }
  42. },
  43. // override parse (from backbone) to extract results
  44. parse: function (response) {
  45. var data = response.data;
  46. this.totalPages = Math.ceil(response.count / this.perPage);
  47. return data;
  48. },
  49. // override (from paginator) in order to add loading mask
  50. pager: function() {
  51. $("body").mask("Loading...");
  52. this.fetch({
  53. success: function(collection, response) {
  54. $("body").unmask();
  55. },
  56. error: function(collection, response) {
  57. $("body").unmask();
  58. }
  59. });
  60. }
  61. });
  62. return BaseCollection;
  63. });