Adding a keepData option to formats. If true, formats will be given a reference to the parsed data (named data) after a read. Use this when you need to access part of the structure that was not extracted in creating features (for example). Note that for versioned parsers, the data property is available on format.parser.data. Thanks for the patch sky. r=me (closes #1753)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8924 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-03-01 16:37:07 +00:00
parent c7afe8e134
commit fecda8f089
5 changed files with 51 additions and 0 deletions

View File

@@ -47,6 +47,20 @@ OpenLayers.Format = OpenLayers.Class({
*/ */
internalProjection: null, internalProjection: null,
/**
* APIProperty: data
* {Object} When <keepData> is true, this is the parsed string sent to
* <read>.
*/
data: null,
/**
* APIProperty: keepData
* {Object} Maintain a reference (<data>) to the most recently read data.
* Default is false.
*/
keepData: false,
/** /**
* Constructor: OpenLayers.Format * Constructor: OpenLayers.Format
* Instances of this class are not useful. See one of the subclasses. * 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 * options - {Object} An optional object with properties to set on the
* format * format
* *
* Valid options:
* keepData - {Boolean} If true, upon <read>, the data property will be
* set to the parsed object (e.g. the json or xml object).
*
* Returns: * Returns:
* An instance of OpenLayers.Format * An instance of OpenLayers.Format
*/ */

View File

@@ -126,6 +126,11 @@ OpenLayers.Format.JSON = OpenLayers.Class(OpenLayers.Format, {
} }
object = walk('', object); object = walk('', object);
} }
if(this.keepData) {
this.data = object;
}
return object; return object;
} }
} catch(e) { } catch(e) {

View File

@@ -161,6 +161,11 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
return req.responseXML; return req.responseXML;
} }
); );
if(this.keepData) {
this.data = node;
}
return node; return node;
}, },

View File

@@ -34,6 +34,18 @@
t.eq(data, obj, "writing data to json works."); 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');
}
</script> </script>
</head> </head>
<body> <body>

View File

@@ -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');
}
</script> </script>
</head> </head>
<body> <body>