PagingMemoryProxy.js 2.8 KB

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