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:
@@ -47,6 +47,20 @@ OpenLayers.Format = OpenLayers.Class({
|
||||
*/
|
||||
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
|
||||
* 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 <read>, the data property will be
|
||||
* set to the parsed object (e.g. the json or xml object).
|
||||
*
|
||||
* Returns:
|
||||
* An instance of OpenLayers.Format
|
||||
*/
|
||||
|
||||
@@ -126,6 +126,11 @@ OpenLayers.Format.JSON = OpenLayers.Class(OpenLayers.Format, {
|
||||
}
|
||||
object = walk('', object);
|
||||
}
|
||||
|
||||
if(this.keepData) {
|
||||
this.data = object;
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
} catch(e) {
|
||||
|
||||
@@ -161,6 +161,11 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
|
||||
return req.responseXML;
|
||||
}
|
||||
);
|
||||
|
||||
if(this.keepData) {
|
||||
this.data = node;
|
||||
}
|
||||
|
||||
return node;
|
||||
},
|
||||
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -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>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
Reference in New Issue
Block a user