PagingMemoryProxy.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Paging Memory Proxy, allows to use paging grid with in memory dataset
  3. */
  4. Ext.define('Ext.ux.data.PagingMemoryProxy', {
  5. extend: 'Ext.data.proxy.Memory',
  6. alias: 'proxy.pagingmemory',
  7. alternateClassName: 'Ext.data.PagingMemoryProxy',
  8. constructor: function() {
  9. Ext.log.warn('Ext.ux.data.PagingMemoryProxy functionality has been merged into Ext.data.proxy.Memory by using the enablePaging flag.');
  10. this.callParent(arguments);
  11. },
  12. read : function(operation, callback, scope){
  13. var reader = this.getReader(),
  14. result = reader.read(this.data),
  15. sorters, filters, sorterFn, records;
  16. scope = scope || this;
  17. // filtering
  18. filters = operation.filters;
  19. if (filters.length > 0) {
  20. //at this point we have an array of Ext.util.Filter objects to filter with,
  21. //so here we construct a function that combines these filters by ANDing them together
  22. records = [];
  23. Ext.each(result.records, function(record) {
  24. var isMatch = true,
  25. length = filters.length,
  26. i;
  27. for (i = 0; i < length; i++) {
  28. var filter = filters[i],
  29. fn = filter.filterFn,
  30. scope = filter.scope;
  31. isMatch = isMatch && fn.call(scope, record);
  32. }
  33. if (isMatch) {
  34. records.push(record);
  35. }
  36. }, this);
  37. result.records = records;
  38. result.totalRecords = result.total = records.length;
  39. }
  40. // sorting
  41. sorters = operation.sorters;
  42. if (sorters.length > 0) {
  43. //construct an amalgamated sorter function which combines all of the Sorters passed
  44. sorterFn = function(r1, r2) {
  45. var result = sorters[0].sort(r1, r2),
  46. length = sorters.length,
  47. i;
  48. //if we have more than one sorter, OR any additional sorter functions together
  49. for (i = 1; i < length; i++) {
  50. result = result || sorters[i].sort.call(this, r1, r2);
  51. }
  52. return result;
  53. };
  54. result.records.sort(sorterFn);
  55. }
  56. // paging (use undefined cause start can also be 0 (thus false))
  57. if (operation.start !== undefined && operation.limit !== undefined) {
  58. result.records = result.records.slice(operation.start, operation.start + operation.limit);
  59. result.count = result.records.length;
  60. }
  61. Ext.apply(operation, {
  62. resultSet: result
  63. });
  64. operation.setCompleted();
  65. operation.setSuccessful();
  66. Ext.defer(function () {
  67. Ext.callback(callback, scope, [operation]);
  68. }, 10);
  69. }
  70. });