Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / examples / ux / data / PagingMemoryProxy.js
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
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
22             Ext.each(result.records, function(record) {
23                 var isMatch = true,
24                     length = filters.length,
25                     i;
26
27                 for (i = 0; i < length; i++) {
28                     var filter = filters[i],
29                         fn     = filter.filterFn,
30                         scope  = filter.scope;
31
32                     isMatch = isMatch && fn.call(scope, record);
33                 }
34                 if (isMatch) {
35                     records.push(record);
36                 }
37             }, this);
38
39             result.records = records;
40         }
41         
42         // sorting
43         sorters = operation.sorters;
44         if (sorters.length > 0) {
45             //construct an amalgamated sorter function which combines all of the Sorters passed
46             sorterFn = function(r1, r2) {
47                 var result = sorters[0].sort(r1, r2),
48                     length = sorters.length,
49                     i;
50                 
51                     //if we have more than one sorter, OR any additional sorter functions together
52                     for (i = 1; i < length; i++) {
53                         result = result || sorters[i].sort.call(this, r1, r2);
54                     }                
55                
56                 return result;
57             };
58     
59             result.records.sort(sorterFn);
60         }
61         
62         // paging (use undefined cause start can also be 0 (thus false))
63         if (operation.start !== undefined && operation.limit !== undefined) {
64             result.records = result.records.slice(operation.start, operation.start + operation.limit);
65         }
66
67         Ext.apply(operation, {
68             resultSet: result
69         });
70         
71         operation.setCompleted();
72         operation.setSuccessful();
73
74         Ext.callback(callback, scope || me, [operation]);
75     }
76 });
77
78 //backwards compat.
79 Ext.data.PagingMemoryProxy = Ext.ux.data.PagingMemoryProxy;