1 | /** |
---|
2 | * Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
3 | * |
---|
4 | * Published under the BSD license. |
---|
5 | * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text |
---|
6 | * of the license. |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @include GeoExt/data/FeatureRecord.js |
---|
11 | */ |
---|
12 | |
---|
13 | /** api: (define) |
---|
14 | * module = GeoExt.data |
---|
15 | * class = FeatureReader |
---|
16 | * base_link = `Ext.data.DataReader <http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.DataReader>`_ |
---|
17 | */ |
---|
18 | Ext.namespace('GeoExt', 'GeoExt.data'); |
---|
19 | |
---|
20 | /** api: example |
---|
21 | * Typical usage in a store: |
---|
22 | * |
---|
23 | * .. code-block:: javascript |
---|
24 | * |
---|
25 | * var store = new Ext.data.Store({ |
---|
26 | * reader: new GeoExt.data.FeatureReader({}, [ |
---|
27 | * {name: 'name', type: 'string'}, |
---|
28 | * {name: 'elevation', type: 'float'} |
---|
29 | * ]) |
---|
30 | * }); |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | /** api: constructor |
---|
35 | * .. class:: FeatureReader(meta, recordType) |
---|
36 | * |
---|
37 | * Data reader class to create an array of |
---|
38 | * :class:`GeoExt.data.FeatureRecord` objects from an |
---|
39 | * ``OpenLayers.Protocol.Response`` object for use in a |
---|
40 | * :class:`GeoExt.data.FeatureStore` object. |
---|
41 | */ |
---|
42 | GeoExt.data.FeatureReader = function(meta, recordType) { |
---|
43 | meta = meta || {}; |
---|
44 | if(!(recordType instanceof Function)) { |
---|
45 | recordType = GeoExt.data.FeatureRecord.create( |
---|
46 | recordType || meta.fields || {}); |
---|
47 | } |
---|
48 | GeoExt.data.FeatureReader.superclass.constructor.call( |
---|
49 | this, meta, recordType); |
---|
50 | }; |
---|
51 | |
---|
52 | Ext.extend(GeoExt.data.FeatureReader, Ext.data.DataReader, { |
---|
53 | |
---|
54 | /** |
---|
55 | * APIProperty: totalRecords |
---|
56 | * {Integer} |
---|
57 | */ |
---|
58 | totalRecords: null, |
---|
59 | |
---|
60 | /** private: method[read] |
---|
61 | * :param response: ``OpenLayers.Protocol.Response`` |
---|
62 | * :return: ``Object`` An object with two properties. The value of the |
---|
63 | * ``records`` property is the array of records corresponding to |
---|
64 | * the features. The value of the ``totalRecords" property is the |
---|
65 | * number of records in the array. |
---|
66 | * |
---|
67 | * This method is only used by a DataProxy which has retrieved data. |
---|
68 | */ |
---|
69 | read: function(response) { |
---|
70 | return this.readRecords(response.features); |
---|
71 | }, |
---|
72 | |
---|
73 | /** api: method[readRecords] |
---|
74 | * :param features: ``Array(OpenLayers.Feature.Vector)`` List of |
---|
75 | * features for creating records |
---|
76 | * :return: ``Object`` An object with ``records`` and ``totalRecords`` |
---|
77 | * properties. |
---|
78 | * |
---|
79 | * Create a data block containing :class:`GeoExt.data.FeatureRecord` |
---|
80 | * objects from an array of features. |
---|
81 | */ |
---|
82 | readRecords : function(features) { |
---|
83 | var records = []; |
---|
84 | |
---|
85 | if (features) { |
---|
86 | var recordType = this.recordType, fields = recordType.prototype.fields; |
---|
87 | var i, lenI, j, lenJ, feature, values, field, v; |
---|
88 | for (i = 0, lenI = features.length; i < lenI; i++) { |
---|
89 | feature = features[i]; |
---|
90 | values = {}; |
---|
91 | if (feature.attributes) { |
---|
92 | for (j = 0, lenJ = fields.length; j < lenJ; j++){ |
---|
93 | field = fields.items[j]; |
---|
94 | if (/[\[\.]/.test(field.mapping)) { |
---|
95 | try { |
---|
96 | v = new Function("obj", "return obj." + field.mapping)(feature.attributes); |
---|
97 | } catch(e){ |
---|
98 | v = field.defaultValue; |
---|
99 | } |
---|
100 | } |
---|
101 | else { |
---|
102 | v = feature.attributes[field.mapping || field.name] || field.defaultValue; |
---|
103 | } |
---|
104 | if (field.convert) { |
---|
105 | v = field.convert(v); |
---|
106 | } |
---|
107 | values[field.name] = v; |
---|
108 | } |
---|
109 | } |
---|
110 | values.feature = feature; |
---|
111 | values.state = feature.state; |
---|
112 | values.fid = feature.fid; |
---|
113 | |
---|
114 | // newly inserted features need to be made into phantom records |
---|
115 | var id = (feature.state === OpenLayers.State.INSERT) ? undefined : feature.id; |
---|
116 | records[records.length] = new recordType(values, id); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | return { |
---|
121 | records: records, |
---|
122 | totalRecords: this.totalRecords != null ? this.totalRecords : records.length |
---|
123 | }; |
---|
124 | } |
---|
125 | }); |
---|