BaseCollection.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. define([ "require", "utils/Globals", "utils/Utils", "utils/CommonViewFunction", "backbone.paginator" ], function(require, Globals, Utils, CommonViewFunction) {
  2. "use strict";
  3. var BaseCollection = Backbone.PageableCollection.extend({
  4. initialize: function() {
  5. this.sort_key = "id";
  6. },
  7. comparator: function(key, value) {
  8. return key = key.get(this.sort_key), value = value.get(this.sort_key), key > value ? 1 : key < value ? -1 : 0;
  9. },
  10. sortByKey: function(sortKey) {
  11. this.sort_key = sortKey, this.sort();
  12. },
  13. state: {
  14. firstPage: 0,
  15. pageSize: Globals.settings.PAGE_SIZE
  16. },
  17. mode: "client",
  18. parseRecords: function(resp, options) {
  19. this.responseData = {
  20. dataType: resp.dataType,
  21. query: resp.query,
  22. queryType: resp.queryType,
  23. requestId: resp.requestId
  24. };
  25. try {
  26. if (!this.modelAttrName) throw new Error("this.modelAttrName not defined for " + this);
  27. return resp[this.modelAttrName];
  28. } catch (e) {
  29. console.log(e);
  30. }
  31. },
  32. getFirstPage: function(options) {
  33. return this.getPage("first", _.extend({
  34. reset: !0
  35. }, options));
  36. },
  37. getPreviousPage: function(options) {
  38. return this.getPage("prev", _.extend({
  39. reset: !0
  40. }, options));
  41. },
  42. getNextPage: function(options) {
  43. return this.getPage("next", _.extend({
  44. reset: !0
  45. }, options));
  46. },
  47. getLastPage: function(options) {
  48. return this.getPage("last", _.extend({
  49. reset: !0
  50. }, options));
  51. },
  52. hasPrevious: function(options) {
  53. return this.hasPreviousPage();
  54. },
  55. hasNext: function(options) {
  56. return this.hasNextPage();
  57. }
  58. }, {
  59. getTableCols: function(cols, collection, defaultSortDirection) {
  60. var retCols = _.map(cols, function(v, k, l) {
  61. var defaults = collection.constructor.tableCols[k];
  62. return defaults || (defaults = {}), _.extend({
  63. name: k,
  64. direction: defaultSortDirection ? defaultSortDirection : null
  65. }, defaults, v);
  66. });
  67. return retCols;
  68. },
  69. nonCrudOperation: function(url, requestMethod, options) {
  70. return options.beforeSend = CommonViewFunction.addRestCsrfCustomHeader, options.data && "object" == typeof options.data && (options.data = JSON.stringify(options.data)),
  71. Backbone.sync.call(this, null, this, _.extend({
  72. url: url,
  73. type: requestMethod
  74. }, options));
  75. }
  76. });
  77. return BaseCollection;
  78. });