diff --git a/lib/OpenLayers/Format.js b/lib/OpenLayers/Format.js index 0faf7170ec..4b5a1226eb 100644 --- a/lib/OpenLayers/Format.js +++ b/lib/OpenLayers/Format.js @@ -47,6 +47,20 @@ OpenLayers.Format = OpenLayers.Class({ */ internalProjection: null, + /** + * APIProperty: data + * {Object} When is true, this is the parsed string sent to + * . + */ + data: null, + + /** + * APIProperty: keepData + * {Object} Maintain a reference () to the most recently read data. + * Default is false. + */ + keepData: false, + /** * Constructor: OpenLayers.Format * Instances of this class are not useful. See one of the subclasses. @@ -55,6 +69,10 @@ OpenLayers.Format = OpenLayers.Class({ * options - {Object} An optional object with properties to set on the * format * + * Valid options: + * keepData - {Boolean} If true, upon , the data property will be + * set to the parsed object (e.g. the json or xml object). + * * Returns: * An instance of OpenLayers.Format */ diff --git a/lib/OpenLayers/Format/JSON.js b/lib/OpenLayers/Format/JSON.js index 6533ffbd6f..609b7b03b2 100644 --- a/lib/OpenLayers/Format/JSON.js +++ b/lib/OpenLayers/Format/JSON.js @@ -126,6 +126,11 @@ OpenLayers.Format.JSON = OpenLayers.Class(OpenLayers.Format, { } object = walk('', object); } + + if(this.keepData) { + this.data = object; + } + return object; } } catch(e) { diff --git a/lib/OpenLayers/Format/XML.js b/lib/OpenLayers/Format/XML.js index 95c3633e8f..7dbdcd46cb 100644 --- a/lib/OpenLayers/Format/XML.js +++ b/lib/OpenLayers/Format/XML.js @@ -161,6 +161,11 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, { return req.responseXML; } ); + + if(this.keepData) { + this.data = node; + } + return node; }, diff --git a/tests/Format/JSON.html b/tests/Format/JSON.html index cd597d712b..0bdbb6fed2 100644 --- a/tests/Format/JSON.html +++ b/tests/Format/JSON.html @@ -34,6 +34,18 @@ t.eq(data, obj, "writing data to json works."); } + + function test_keepData(t) { + t.plan(2); + + var options = {'keepData': true}; + var format = new OpenLayers.Format.JSON(options); + format.read('{"a":["b"], "c":1}'); + + t.ok(format.data != null, 'data property is not null after read with keepData=true'); + t.eq(format.data.c,1,'keepData keeps the right data'); + } + diff --git a/tests/Format/XML.html b/tests/Format/XML.html index 24a3952a11..94d67206d6 100644 --- a/tests/Format/XML.html +++ b/tests/Format/XML.html @@ -668,6 +668,17 @@ } + function test_keepData(t) { + t.plan(2); + + var options = {'keepData': true}; + var format = new OpenLayers.Format.XML(options); + format.read(text); + + t.ok(format.data != null, 'data property is not null after read with keepData=true'); + t.eq(format.data.documentElement.tagName,'ol:root','keepData keeps the right data'); + } +