Changeset 260


Ignore:
Timestamp:
03/24/09 09:17:27 (9 years ago)
Author:
elemoine
Message:

add LayerReader, r=ahocevar (closes #25, #27)

Location:
core/trunk
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • core/trunk/lib/GeoExt.js

    r238 r260  
    6161            "GeoExt/data/FeatureReader.js",
    6262            "GeoExt/data/FeatureStoreMediator.js",
     63            "GeoExt/data/LayerRecord.js",
     64            "GeoExt/data/LayerReader.js",
    6365            "GeoExt/data/LayerStore.js",
    64             "GeoExt/data/LayerRecord.js",
    6566            "GeoExt/data/LayerStoreMediator.js",
    6667            "GeoExt/data/ProtocolProxy.js",
  • core/trunk/lib/GeoExt/data/LayerRecord.js

    r238 r260  
    1 /* Copyright (C) 2008-2009 The Open Source Geospatial Foundation Â¹
     1/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation
    22 * Published under the BSD license.
    33 * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
    44 * of the license.
    55 *
    6  * ¹ pending approval */
     6 * pending approval */
    77
    88Ext.namespace("GeoExt.data");
     
    1818 * - {Ext.data.Record}
    1919 */
    20 GeoExt.data.LayerRecord = Ext.extend(Ext.data.Record, {
    21    
    22     /**
    23      * Constructor: GeoExt.data.LayerRecord
    24      * This constructor should not be used directly. Instead, the create method of
    25      * this class should be used to create a constructor. The parameters are the
    26      * same.
    27      *
    28      * Parameters:
    29      * layer - {OpenLayers.Layer}
    30      * data - {Object} additional values, keyed by field name
    31      */
    32     constructor: function(layer, data) {
    33         this.data = Ext.applyIf({
    34             title: layer.name,
    35             layer: layer
    36         }, data);
    37         this.id = layer.id;
    38     }
    39 });
     20GeoExt.data.LayerRecord = Ext.data.Record.create([
     21    {name: "layer"},
     22    {name: "title", type: "string", mapping: "name"}
     23]);
    4024
    4125/**
     
    4529 *
    4630 * Parameters:
    47  * fieldDefinition - {Array} Field definition as in {Ext.data.Record.create}.
    48  *     Can be omitted if no additional fields are required (records will
    49  *     always have a {OpenLayers.Layer} layer and a {String} title field).
     31 * o - {Array} Field definition as in {Ext.data.Record.create}. Can be omitted
     32 *     if no additional fields are required (records will always have a
     33 *     {OpenLayers.Layer} layer and a {String} title field).
     34 *
     35 * Returns:
     36 * {Function} A specialized {<GeoExt.data.LayerRecord>} constructor.
    5037 */
    51 GeoExt.data.LayerRecord.create = function(fieldDefinition) {
    52     var o = [
    53         {name: "layer"},
    54         {name: "title"}
    55     ].concat(fieldDefinition);
    56 
     38GeoExt.data.LayerRecord.create = function(o) {
    5739    var f = Ext.extend(GeoExt.data.LayerRecord, {});
    5840    var p = f.prototype;
    59     p.fields = new Ext.util.MixedCollection(false, function(field){
    60         return field.name;
    61     });
    62     for(var i = 0, len = o.length; i < len; i++){
    63         p.fields.add(new Ext.data.Field(o[i]));
     41    if(o) {
     42        for(var i = 0, len = o.length; i < len; i++){
     43            p.fields.add(new Ext.data.Field(o[i]));
     44        }
    6445    }
    65     f.getField = function(name){
    66         return p.fields.get(name);
    67     };
    6846    return f;
    6947}
  • core/trunk/lib/GeoExt/data/LayerStore.js

    r238 r260  
    3838     */
    3939    map: null,
    40    
     40
    4141    /**
    42      * APIProperty: recordType
    43      * {<GeoExt.data.LayerRecord>} type of the layer records used by this
    44      *     store.
     42     * Property: reader
     43     * {<GeoExt.data.LayerReader>} The reader used to get
     44     *     <GeoExt.data.LayerRecord> objects from {OpenLayers.Layer}
     45     *     objects.
    4546     */
    46     recordType: null,
     47    reader: null,
    4748
    4849    /**
     
    6465        arguments.callee.superclass.constructor.apply(this, arguments);
    6566        config = config || {};
    66         this.recordType = config.recordType || GeoExt.data.LayerRecord.create();
     67        this.reader = new GeoExt.data.LayerReader({}, config.recordType);
    6768        var map = config.map instanceof GeoExt.MapPanel ?
    6869            config.map.map : config.map;
     
    7475            for(var i=0; i<layers.length; ++i) {
    7576                layer = layers[i];
    76                 this.add(new this.recordType(layer));
     77                this.add((this.reader.readRecords([layer])).records);
    7778            }
    7879
     
    112113        var layer = evt.layer;
    113114        this._adding = true;
    114         this.add(new this.recordType(layer));
     115        this.add((this.reader.readRecords([layer])).records);
    115116        delete this._adding;
    116117    },
  • core/trunk/tests/data/LayerRecord.html

    r238 r260  
    1010       
    1111        function test_layerrecord(t) {
    12             t.plan(4);
     12            t.plan(12);
     13
     14            var c, layer, record;
    1315           
    14             var c = GeoExt.data.LayerRecord.create();
    15            
    16             var layer = new OpenLayers.Layer();
    17             var record = new c(layer);
     16            c = GeoExt.data.LayerRecord.create();
     17            t.ok(c instanceof Function, "create returns a func");
     18            t.eq(c.prototype.fields.items[0].name, "layer",
     19                 "\"create\" returns a func with a \"layer\" field in its prototype");
     20            t.eq(c.prototype.fields.items[1].name, "title",
     21                 "\"create\" returns a func with a \"title\" field in its prototype");
    1822
    19             t.ok(record instanceof GeoExt.data.LayerRecord, "create returns a constructor");
    20             t.eq(record.get("layer").id, layer.id, "Layer stored correctly");
    21             t.eq(record.id, layer.id, "ID set correctly");
    22            
    23             var record = new c(layer, {"foo": "bar"});
     23            c = GeoExt.data.LayerRecord.create([
     24                {name: "extra1"}, {name: "extra2"}
     25            ]);
     26            t.eq(c.prototype.fields.items[0].name, "layer",
     27                 "\"create(o)\" returns a func with a \"layer\" field in its prototype");
     28            t.eq(c.prototype.fields.items[1].name, "title",
     29                 "\"create(o)\" returns a func with a \"title\" field in its prototype");
     30            t.eq(c.prototype.fields.items[2].name, "extra1",
     31                 "\"create(o)\" returns a func with a \"extra1\" field in its prototype");
     32            t.eq(c.prototype.fields.items[3].name, "extra2",
     33                 "\"create(o)\" returns a func with a \"extra2\" field in its prototype");
     34
     35            layer = new OpenLayers.Layer();
     36            record = new c({layer: layer, title: layer.name}, layer.id);
     37            t.ok(record instanceof GeoExt.data.LayerRecord, "create returns a constructor (LayerRecord)");
     38            t.ok(record instanceof c, "create returns a constructor (c)");
     39            t.eq(record.get("layer").id, layer.id, "layer stored correctly");
     40            t.eq(record.id, layer.id, "id set correctly");
     41            record = new c({layer: layer, title: layer.name, foo: "bar"}, layer.id);
    2442            t.eq(record.get("foo"), "bar", "foo data row set correctly");
    2543        }
  • core/trunk/tests/data/LayerStore.html

    r238 r260  
    1717            var map = createMap();
    1818
    19             mapPanel = new GeoExt.MapPanel({
     19            var mapPanel = new GeoExt.MapPanel({
    2020                // panel options
    2121                id: "map-panel",
     
    4242
    4343            map.addLayer(layer);
    44 
    45             t.eq(map.layers.length,1,"Adding layer to map does not create duplicate layers on map");
    46             t.eq(mapPanel.layers.getCount(),1,"Adding layer to map does not create duplicate records in LayerStore");
     44            t.eq(map.layers.length, 1, "Adding layer to map does not create duplicate layers on map");
     45            t.eq(mapPanel.layers.getCount(), 1, "Adding layer to map does not create duplicate records in LayerStore");
    4746
    4847            mapPanel.layers.remove(mapPanel.layers.getById(layer.id));
    49 
    5048            t.eq(map.layers.length,0,"removeLayer on MapPanel's LayerStore removes layer from map");
    5149            t.eq(mapPanel.layers.getCount(),0,"removeLayer on MapPanel's LayerStore removes layer from map");
    5250
    53             mapPanel.layers.add(new GeoExt.data.LayerRecord(layer));
     51            var reader = new GeoExt.data.LayerReader();
     52            mapPanel.layers.add((reader.readRecords([layer])).records);
    5453            t.eq(map.layers.length,1,"Adding layer to MapPanel's LayerStore adds only one layer to map");
    5554            t.eq(mapPanel.layers.getCount(),1,"Adding layers to MapPanel's LayerStore does not create duplicate layers");
    56 
    57 
    5855        }
    5956    </script>
  • core/trunk/tests/list-tests.html

    r238 r260  
    33  <li>data/FeatureStoreMediator.html</li>
    44  <li>data/LayerRecord.html</li>
     5  <li>data/LayerReader.html</li>
    56  <li>data/LayerStore.html</li>
    67  <li>data/LayerStoreMediator.html</li>
Note: See TracChangeset for help on using the changeset viewer.