Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / ux / data / PagingMemoryProxy.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.ux.data.PagingMemoryProxy
17  * @extends Ext.data.proxy.Memory
18  * <p>Paging Memory Proxy, allows to use paging grid with in memory dataset</p>
19  */
20 Ext.define('Ext.ux.data.PagingMemoryProxy', {
21     extend: 'Ext.data.proxy.Memory',
22     alias: 'proxy.pagingmemory',
23     alternateClassName: 'Ext.data.PagingMemoryProxy',
24
25     read : function(operation, callback, scope){
26         var reader = this.getReader(),
27             result = reader.read(this.data),
28             sorters, filters, sorterFn, records;
29
30         scope = scope || this;
31         // filtering
32         filters = operation.filters;
33         if (filters.length > 0) {
34             //at this point we have an array of  Ext.util.Filter objects to filter with,
35             //so here we construct a function that combines these filters by ANDing them together
36             records = [];
37
38             Ext.each(result.records, function(record) {
39                 var isMatch = true,
40                     length = filters.length,
41                     i;
42
43                 for (i = 0; i < length; i++) {
44                     var filter = filters[i],
45                         fn     = filter.filterFn,
46                         scope  = filter.scope;
47
48                     isMatch = isMatch && fn.call(scope, record);
49                 }
50                 if (isMatch) {
51                     records.push(record);
52                 }
53             }, this);
54
55             result.records = records;
56             result.totalRecords = result.total = records.length;
57         }
58         
59         // sorting
60         sorters = operation.sorters;
61         if (sorters.length > 0) {
62             //construct an amalgamated sorter function which combines all of the Sorters passed
63             sorterFn = function(r1, r2) {
64                 var result = sorters[0].sort(r1, r2),
65                     length = sorters.length,
66                     i;
67                 
68                     //if we have more than one sorter, OR any additional sorter functions together
69                     for (i = 1; i < length; i++) {
70                         result = result || sorters[i].sort.call(this, r1, r2);
71                     }                
72                
73                 return result;
74             };
75     
76             result.records.sort(sorterFn);
77         }
78         
79         // paging (use undefined cause start can also be 0 (thus false))
80         if (operation.start !== undefined && operation.limit !== undefined) {
81             result.records = result.records.slice(operation.start, operation.start + operation.limit);
82             result.count = result.records.length;
83         }
84
85         Ext.apply(operation, {
86             resultSet: result
87         });
88         
89         operation.setCompleted();
90         operation.setSuccessful();
91
92         Ext.Function.defer(function () {
93             Ext.callback(callback, scope, [operation]);
94         }, 10);
95     }
96 });
97