[Commits] r259 - in sandbox/elemoine/playground: lib lib/GeoExt/data tests tests/data

commits at geoext.org commits at geoext.org
Tue Mar 24 06:48:29 CET 2009


Author: elemoine
Date: 2009-03-24 06:48:28 +0100 (Tue, 24 Mar 2009)
New Revision: 259

Added:
   sandbox/elemoine/playground/lib/GeoExt/data/FeatureRecord.js
   sandbox/elemoine/playground/tests/data/FeatureRecord.html
Modified:
   sandbox/elemoine/playground/lib/GeoExt.js
   sandbox/elemoine/playground/lib/GeoExt/data/FeatureReader.js
   sandbox/elemoine/playground/tests/data/FeatureReader.html
   sandbox/elemoine/playground/tests/data/FeatureStore.html
   sandbox/elemoine/playground/tests/list-tests.html
Log:
add FeatureRecord (#28)


Modified: sandbox/elemoine/playground/lib/GeoExt/data/FeatureReader.js
===================================================================
--- sandbox/elemoine/playground/lib/GeoExt/data/FeatureReader.js	2009-03-23 23:41:23 UTC (rev 258)
+++ sandbox/elemoine/playground/lib/GeoExt/data/FeatureReader.js	2009-03-24 05:48:28 UTC (rev 259)
@@ -46,9 +46,12 @@
  */
 GeoExt.data.FeatureReader = function(meta, recordType) {
     meta = meta || {};
+    if(!(recordType instanceof Function)) {
+        recordType = GeoExt.data.FeatureRecord.create(
+            recordType || meta.fields || {});
+    }
     GeoExt.data.FeatureReader.superclass.constructor.call(
-        this, meta, recordType || meta.fields
-    );
+        this, meta, recordType);
 };
 
 Ext.extend(GeoExt.data.FeatureReader, Ext.data.DataReader, {

Added: sandbox/elemoine/playground/lib/GeoExt/data/FeatureRecord.js
===================================================================
--- sandbox/elemoine/playground/lib/GeoExt/data/FeatureRecord.js	                        (rev 0)
+++ sandbox/elemoine/playground/lib/GeoExt/data/FeatureRecord.js	2009-03-24 05:48:28 UTC (rev 259)
@@ -0,0 +1,46 @@
+/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation
+ * Published under the BSD license.
+ * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
+ * of the license.
+ * 
+ * pending approval */
+
+Ext.namespace("GeoExt.data");
+
+/**
+ * Class: GeoExt.data.FeatureRecord
+ * A subclass of {Ext.data.Record} which provides a special record that
+ * represents an {OpenLayers.Feature}. This record will always have at
+ * least "feature", "fid", and "state" fields in its data. The id of
+ * the record will be the same as the id of the feature it represents.
+ * 
+ * Inherits from
+ * - {Ext.data.Record}
+ */
+GeoExt.data.FeatureRecord = Ext.data.Record.create([
+    {name: "feature"}, {name: "state"}, {name: "fid"}
+]);
+
+/**
+ * APIFunction: GeoExt.data.FeatureRecord.create
+ * Creates a constructor for a FeatureRecord, optionally with additional
+ * fields.
+ * 
+ * Parameters:
+ * o - {Array} Field definition as in {Ext.data.Record.create}. Can be omitted
+ *     if no additional fields are required (records will always have fields
+ *     {OpenLayers.Feature} "feature", {String} "state" and {String} "fid".
+ *
+ * Returns:
+ * {Function} A specialized {<GeoExt.data.FeatureRecord>} constructor.
+ */
+GeoExt.data.FeatureRecord.create = function(o) {
+    var f = Ext.extend(GeoExt.data.FeatureRecord, {});
+    var p = f.prototype;
+    if(o) {
+        for(var i = 0, len = o.length; i < len; i++){
+            p.fields.add(new Ext.data.Field(o[i]));
+        }
+    }
+    return f;
+}

Modified: sandbox/elemoine/playground/lib/GeoExt.js
===================================================================
--- sandbox/elemoine/playground/lib/GeoExt.js	2009-03-23 23:41:23 UTC (rev 258)
+++ sandbox/elemoine/playground/lib/GeoExt.js	2009-03-24 05:48:28 UTC (rev 259)
@@ -58,6 +58,7 @@
      */
     if(!singleFile) {
         var jsfiles = new Array(
+            "GeoExt/data/FeatureRecord.js",
             "GeoExt/data/FeatureReader.js",
             "GeoExt/data/FeatureStore.js",
             "GeoExt/data/LayerRecord.js",

Modified: sandbox/elemoine/playground/tests/data/FeatureReader.html
===================================================================
--- sandbox/elemoine/playground/tests/data/FeatureReader.html	2009-03-23 23:41:23 UTC (rev 258)
+++ sandbox/elemoine/playground/tests/data/FeatureReader.html	2009-03-24 05:48:28 UTC (rev 259)
@@ -16,9 +16,9 @@
             ]);
             var fields = reader.recordType.prototype.fields;
             // 2 tests
-            t.eq(fields.items.length, 2, 'number of items is correct');
-            t.ok(fields.items[0].name == 'foo' &&
-                 fields.items[1].name == 'bar',
+            t.eq(fields.items.length, 5, 'number of items is correct');
+            t.ok(fields.items[3].name == 'foo' &&
+                 fields.items[4].name == 'bar',
                  'field values are correct');
         }
         function test_readRecords(t) {

Added: sandbox/elemoine/playground/tests/data/FeatureRecord.html
===================================================================
--- sandbox/elemoine/playground/tests/data/FeatureRecord.html	                        (rev 0)
+++ sandbox/elemoine/playground/tests/data/FeatureRecord.html	2009-03-24 05:48:28 UTC (rev 259)
@@ -0,0 +1,52 @@
+<!DOCTYPE html>
+<html debug="true">
+  <head>
+    <script type="text/javascript" src="../../../../openlayers/lib/OpenLayers.js"></script>
+    <script type="text/javascript" src="../../../../ext/2.2.1/adapter/ext/ext-base.js"></script>
+    <script type="text/javascript" src="../../../../ext/2.2.1/ext-all-debug.js"></script>
+    <script type="text/javascript" src="../../lib/GeoExt.js"></script>
+
+    <script type="text/javascript">
+       
+        function test_featurerecord(t) {
+            t.plan(14);
+
+            var c, feature, record;
+            
+            c = GeoExt.data.FeatureRecord.create();
+            t.ok(c instanceof Function, "create returns a func");
+            t.eq(c.prototype.fields.items[0].name, "feature",
+                 "\"create\" returns a func with a \"feature\" field in its prototype"); 
+            t.eq(c.prototype.fields.items[1].name, "state",
+                 "\"create\" returns a func with a \"state\" field in its prototype"); 
+            t.eq(c.prototype.fields.items[2].name, "fid",
+                 "\"create\" returns a func with a \"fid\" field in its prototype"); 
+
+            c = GeoExt.data.FeatureRecord.create([
+                {name: "extra1"}, {name: "extra2"}
+            ]);
+            t.eq(c.prototype.fields.items[0].name, "feature",
+                 "\"create(o)\" returns a func with a \"feature\" field in its prototype"); 
+            t.eq(c.prototype.fields.items[1].name, "state",
+                 "\"create(o)\" returns a func with a \"state\" field in its prototype"); 
+            t.eq(c.prototype.fields.items[2].name, "fid",
+                 "\"create(o)\" returns a func with a \"fid\" field in its prototype"); 
+            t.eq(c.prototype.fields.items[3].name, "extra1",
+                 "\"create(o)\" returns a func with a \"extra1\" field in its prototype"); 
+            t.eq(c.prototype.fields.items[4].name, "extra2",
+                 "\"create(o)\" returns a func with a \"extra2\" field in its prototype"); 
+
+            feature = new OpenLayers.Feature();
+            record = new c({feature: feature, state: feature.state, fid: feature.fid}, feature.id);
+            t.ok(record instanceof GeoExt.data.FeatureRecord, "create returns a constructor (FeatureRecord)");
+            t.ok(record instanceof c, "create returns a constructor (c)");
+            t.eq(record.get("feature").id, feature.id, "feature stored correctly");
+            t.eq(record.id, feature.id, "id set correctly");
+            record = new c({feature: feature, state: feature.name, fid: feature.fid, foo: "bar"}, feature.id);
+            t.eq(record.get("foo"), "bar", "foo data row set correctly");
+        }
+    </script>
+  <body>
+    <div id="mappanel"></div>
+  </body>
+</html>

Modified: sandbox/elemoine/playground/tests/data/FeatureStore.html
===================================================================
--- sandbox/elemoine/playground/tests/data/FeatureStore.html	2009-03-23 23:41:23 UTC (rev 258)
+++ sandbox/elemoine/playground/tests/data/FeatureStore.html	2009-03-24 05:48:28 UTC (rev 259)
@@ -18,9 +18,9 @@
             });
             var fields = store.reader.recordType.prototype.fields;
             // 2 tests
-            t.eq(fields.items.length, 2, 'number of items is correct');
-            t.ok(fields.items[0].name == 'foo' &&
-                 fields.items[1].name == 'bar',
+            t.eq(fields.items.length, 5, 'number of items is correct');
+            t.ok(fields.items[3].name == 'foo' &&
+                 fields.items[4].name == 'bar',
                  'field values are correct');
         }
         function test_manipulateData(t) {

Modified: sandbox/elemoine/playground/tests/list-tests.html
===================================================================
--- sandbox/elemoine/playground/tests/list-tests.html	2009-03-23 23:41:23 UTC (rev 258)
+++ sandbox/elemoine/playground/tests/list-tests.html	2009-03-24 05:48:28 UTC (rev 259)
@@ -1,4 +1,5 @@
 <ul id="testlist">
+  <li>data/FeatureRecord.html</li>
   <li>data/FeatureReader.html</li>
   <li>data/FeatureStore.html</li>
   <li>data/FeatureStoreMediator.html</li>



More information about the Commits mailing list