PagingMemoryProxy.js 2.7 KB

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