diff --git a/lib/OpenLayers/Protocol/WFS/v1.js b/lib/OpenLayers/Protocol/WFS/v1.js index 4a400f1891..94bf2dcc7e 100644 --- a/lib/OpenLayers/Protocol/WFS/v1.js +++ b/lib/OpenLayers/Protocol/WFS/v1.js @@ -74,6 +74,12 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, { */ readFormat: null, + /** + * Property: readOptions + * {Object} Optional object to pass to format's read. + */ + readOptions: null, + /** * Constructor: OpenLayers.Protocol.WFS * A class for giving layers WFS protocol. @@ -173,7 +179,12 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, { var request = response.priv; if(request.status >= 200 && request.status < 300) { // success - response.features = this.parseFeatures(request); + if (this.readOptions && this.readOptions.output == "object") { + OpenLayers.Util.extend(response, + this.parseResponse(request, this.readOptions)); + } else { + response.features = this.parseResponse(request); + } response.code = OpenLayers.Protocol.Response.SUCCESS; } else { // failure @@ -184,17 +195,20 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, { }, /** - * Method: parseFeatures + * Method: parseResponse * Read HTTP response body and return features * * Parameters: * request - {XMLHttpRequest} The request object + * options - {Object} Optional object to pass to format's read * * Returns: - * {Array({})} or - * {} Array of features or a single feature. + * {Object} or {Array({})} or + * {} + * An object with a features property, an array of features or a single + * feature. */ - parseFeatures: function(request) { + parseResponse: function(request, options) { var doc = request.responseXML; if(!doc || !doc.documentElement) { doc = request.responseText; @@ -203,7 +217,7 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, { return null; } return (this.readFormat !== null) ? this.readFormat.read(doc) : - this.format.read(doc); + this.format.read(doc, options); }, /** diff --git a/tests/Protocol/WFS.html b/tests/Protocol/WFS.html index 0eb40fefce..a13cbd099f 100644 --- a/tests/Protocol/WFS.html +++ b/tests/Protocol/WFS.html @@ -31,8 +31,8 @@ url: "http://some.url.org", featureNS: "http://namespace.org", featureType: "type", - parseFeatures: function(request) { - t.eq(request.responseText, "foo", "parseFeatures called properly"); + parseResponse: function(request) { + t.eq(request.responseText, "foo", "parseResponse called properly"); return "foo"; } }); @@ -225,10 +225,41 @@ var request = {}; request.responseText = '{"type":"FeatureCollection","features":[{"type":"Feature","id":"V_HECTOPUNTEN.108411","geometry":{"type":"MultiPoint","coordinates":[[190659.467,349576.19]]},"geometry_name":"ORA_GEOMETRY","properties":{"WEGNUMMER":"002","HECTOMTRNG_ORG":2200,"HECTOMTRNG":"220.00","bbox":[190659.467,349576.19,190659.467,349576.19]}}]}'; - var features = protocol.parseFeatures(request); + var features = protocol.parseResponse(request); t.eq(features.length, 1, "the right format is used to read the request (GeoJSON)"); } + function test_readOptions(t) { + t.plan(1); + + var protocol = new OpenLayers.Protocol.WFS({ + url: "http://some.url.org", + version: "1.1.0", + featureNS: "http://namespace.org", + featureType: "type", + readOptions: {'output': 'object'}, + parseResponse: function(request, options) { + t.eq(options.output, "object", "Options object correctly set to pass on to Format's read"); + } + }); + + var _POST = OpenLayers.Request.POST; + + OpenLayers.Request.POST = function(obj) { + obj.status = 200; + obj.responseText = "foo"; + obj.options = {}; + t.delay_call(0.1, function() {obj.callback.call(this)}); + return obj; + }; + + protocol.read({ + callback: function() {} + }); + + OpenLayers.Request.POST = _POST; + } + function readXML(id) { var xml = document.getElementById(id).firstChild.nodeValue; return new OpenLayers.Format.XML().read(xml).documentElement;