/web/js/modules/store-clients/collections/customers.js

https://github.com/seosamba/ecommerce · JavaScript · 83 lines · 81 code · 2 blank · 0 comment · 10 complexity · f0a137fcb38c406b8b9f5bdc022067e7 MD5 · raw file

  1. define([
  2. 'backbone',
  3. '../models/customer_row'
  4. ], function(Backbone, CustomerRowModel){
  5. var CustomersCollection = Backbone.Collection.extend({
  6. model: CustomerRowModel,
  7. urlRoot: 'api/store/customers/',
  8. paginator: {
  9. limit: 30,
  10. offset: 0,
  11. last: false
  12. },
  13. order: {
  14. by: 'reg_date',
  15. asc: false
  16. },
  17. searchTerm: '',
  18. roleId: '',
  19. cached: {},
  20. clientsFilter: 'clients-only',
  21. initialize: function(){
  22. this.bind('reset', this.updatePaginator, this);
  23. },
  24. url: function(){
  25. var url = this.urlRoot + 'for/dashboard/',
  26. order = '';
  27. url += '?'+'limit='+this.paginator.limit+'&offset='+this.paginator.offset+'&clientsFilter='+this.clientsFilter;
  28. if (this.order.by) {
  29. url += '&order=' + this.order.by + ' ' + (this.order.asc ? 'asc' : 'desc');
  30. }
  31. if (this.searchTerm) {
  32. url += '&search='+this.searchTerm;
  33. }
  34. if (this.roleId) {
  35. url += '&roleId='+this.roleId;
  36. }
  37. return $('#website_url').val() + url + '&id=';
  38. },
  39. next: function(callback) {
  40. if (!this.paginator.last) {
  41. this.paginator.offset += this.paginator.limit;
  42. return this.fetch().done(callback);
  43. }
  44. },
  45. previous: function(callback) {
  46. if (this.paginator.offset >= this.paginator.limit){
  47. this.paginator.offset -= this.paginator.limit;
  48. return this.fetch().done(callback);
  49. }
  50. },
  51. updatePaginator: function(collection) {
  52. if (this.length === 0){
  53. this.previous();
  54. } else {
  55. this.paginator.last = (this.length < this.paginator.limit);
  56. }
  57. },
  58. checked: function(){
  59. return this.filter(function(customer){ return customer.has('checked') && customer.get('checked'); });
  60. },
  61. search: function(term){
  62. if (term !== this.searchTerm){
  63. this.searchTerm = escape(term);
  64. this.paginator.offset = 0;
  65. this.paginator.last = false;
  66. this.paginator.order = {by: null,asc: true};
  67. return this.fetch();
  68. }
  69. },
  70. clientsFilterAction: function (term) {
  71. if (term !== this.clientsFilter){
  72. this.clientsFilter = escape(term);
  73. this.paginator.offset = 0;
  74. this.paginator.last = false;
  75. this.paginator.order = {by: null,asc: true};
  76. return this.fetch();
  77. }
  78. }
  79. });
  80. return CustomersCollection;
  81. });