[Commits] r226 - sandbox/opengeo/alachua/lib/GeoExt/data

commits at geoext.org commits at geoext.org
Mon Mar 16 19:48:51 CET 2009


Author: tcoulter
Date: 2009-03-16 19:48:51 +0100 (Mon, 16 Mar 2009)
New Revision: 226

Modified:
   sandbox/opengeo/alachua/lib/GeoExt/data/ProtocolProxy.js
Log:
Adding Dwin's changes to the ProtocolProxy to support caching and filtering.

Modified: sandbox/opengeo/alachua/lib/GeoExt/data/ProtocolProxy.js
===================================================================
--- sandbox/opengeo/alachua/lib/GeoExt/data/ProtocolProxy.js	2009-03-16 15:11:16 UTC (rev 225)
+++ sandbox/opengeo/alachua/lib/GeoExt/data/ProtocolProxy.js	2009-03-16 18:48:51 UTC (rev 226)
@@ -56,6 +56,28 @@
     response: null,
 
     /**
+     * Property: ogcFilter
+     * {<OpenLayers.Protocol.Filter>} The filter to use when querying the server.
+     */
+    ogcFilter: null,
+
+    /**
+     * Method: setOGCFilter
+     * Parameters:
+     * filter - {OpenLayers.Filter} The filter to use when making requests to the server.
+     */
+    setOGCFilter: function(filter){
+        this.ogcFilter = filter;
+        delete this.cachedFeatures;
+    },
+    
+    getOGCFilter: function() {
+        return this.ogcFilter;  
+    },
+
+    cachedFeatures: null,
+
+    /**
      * Method: load
      *
      * Parameters:
@@ -73,26 +95,37 @@
      */
     load: function(params, reader, callback, scope, arg) {
         if (this.fireEvent("beforeload", this, params) !== false) {
-            var o = {
-                params: params || {},
-                request: {
-                    callback: callback,
-                    scope: scope,
-                    arg: arg
-                },
-                reader: reader
-            };
-            var cb = OpenLayers.Function.bind(this.loadResponse, this, o);
-            if (this.abortPrevious) {
-                this.abortRequest();
+            if (!this.cachedFeatures){
+                var o = {
+                    params: params || {},
+                    request: {
+                        callback: callback,
+                        scope: scope,
+                        arg: arg
+                    },
+                    reader: reader
+                };
+                var cb = OpenLayers.Function.bind(this.loadResponse, this, o);
+                if (this.abortPrevious) {
+                    this.abortRequest();
+                }
+                var options = {
+                    params: params,
+                    callback: cb,
+                    scope: this,
+                    filter: this.ogcFilter
+                };
+
+                Ext.applyIf(options, arg);
+                this.response = this.protocol.read(options);
+            } else {
+                var result = reader.readRecords(this.cachedFeatures);
+                if (params) this.sortFeatures(params, result.records);
+                result.records = 
+                    result.records.splice(params.start || 0, params.limit || result.records.length);
+                this.fireEvent("load", this, o, arg);
+                callback.call(scope, result, arg, true);
             }
-            var options = {
-                params: params,
-                callback: cb,
-                scope: this
-            };
-            Ext.applyIf(options, arg);
-            this.response = this.protocol.read(options);
         } else {
            callback.call(scope || this, null, arg, false);
         }
@@ -126,14 +159,33 @@
      */
     loadResponse: function(o, response) {
         if (response.success()) {
+            this.cachedFeatures = response.features;
+            this.sortFeatures(o.params, response);
             var result = o.reader.read(response);
+            if (o.params) this.sortFeatures(o.params, result.records);
+            result.records = 
+                result.records.splice(o.params.start || 0, o.params.limit || result.records.length);
             this.fireEvent("load", this, o, o.request.arg);
-            o.request.callback.call(
-               o.request.scope, result, o.request.arg, true);
+            o.request.callback.call(o.request.scope, result, o.request.arg, true);
         } else {
             this.fireEvent("loadexception", this, o, response);
-            o.request.callback.call(
-                o.request.scope, null, o.request.arg, false);
+            o.request.callback.call(o.request.scope, null, o.request.arg, false);
         }
+    },
+
+    sortFeatures: function(params, records) {
+        if (params['sort'] && records[0]){
+            var f = params['sort'];
+            var st = records[0].fields.get(f).sortType;
+
+            var comparator = function(a, b){
+                var at = st(a.data[f]);
+                var bt = st(b.data[f]);
+                return at < bt ? -1 : (at > bt ? 1 : 0);
+            }
+
+            records.sort(comparator);
+            if (params['dir'] == 'DESC') records.reverse();
+        }
     }
 });



More information about the Commits mailing list