diff --git a/lib/OpenLayers/Protocol/WFS/v1.js b/lib/OpenLayers/Protocol/WFS/v1.js index 03d8c28ff0..a7ac3251f4 100644 --- a/lib/OpenLayers/Protocol/WFS/v1.js +++ b/lib/OpenLayers/Protocol/WFS/v1.js @@ -142,12 +142,26 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, { }, /** - * Method: read + * APIMethod: read * Construct a request for reading new features. Since WFS splits the * basic CRUD operations into GetFeature requests (for read) and * Transactions (for all others), this method does not make use of the * format's read method (that is only about reading transaction * responses). + * + * To use a configured protocol to get e.g. a WFS hit count, applications + * could do the following: + * + * (code) + * protocol.read({ + * readOptions: {output: "object"}, + * resultType: "hits", + * maxFeatures: null, + * callback: function(resp) { + * // process resp.numberOfFeatures here + * } + * }); + * (end) */ read: function(options) { OpenLayers.Protocol.prototype.read.apply(this, arguments); @@ -180,15 +194,18 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, { * options - {Object} The user options passed to the read call. */ handleRead: function(response, options) { + options = OpenLayers.Util.extend({}, options); + OpenLayers.Util.applyDefaults(options, this.options); + if(options.callback) { var request = response.priv; if(request.status >= 200 && request.status < 300) { // success - if (this.readOptions && this.readOptions.output == "object") { + if (options.readOptions && options.readOptions.output == "object") { OpenLayers.Util.extend(response, - this.parseResponse(request, this.readOptions)); + this.parseResponse(request, options.readOptions)); } else { - response.features = this.parseResponse(request); + response.features = this.parseResponse(request, options.readOptions); } response.code = OpenLayers.Protocol.Response.SUCCESS; } else { diff --git a/tests/Protocol/WFS.html b/tests/Protocol/WFS.html index a13cbd099f..8347932b48 100644 --- a/tests/Protocol/WFS.html +++ b/tests/Protocol/WFS.html @@ -25,14 +25,15 @@ } function test_read(t) { - t.plan(6); + t.plan(7); var protocol = new OpenLayers.Protocol.WFS({ url: "http://some.url.org", featureNS: "http://namespace.org", featureType: "type", - parseResponse: function(request) { + parseResponse: function(request, options) { t.eq(request.responseText, "foo", "parseResponse called properly"); + t.eq(options, {foo: "bar"}, "parseResponse receives readOptions"); return "foo"; } }); @@ -44,14 +45,13 @@ t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "GetFeature request is correct"); obj.status = status; obj.responseText = "foo"; - obj.options = {}; t.delay_call(0.1, function() {obj.callback.call(this)}); return obj; }; expected = readXML("GetFeature_1"); status = 200; - var response = protocol.read({callback: function(response) { + var response = protocol.read({readOptions: {foo: "bar"}, callback: function(response) { t.eq(response.features, "foo", "user callback properly called with features"); t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success reported properly"); }}); @@ -95,7 +95,6 @@ OpenLayers.Request.POST = function(obj) { t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "Transaction XML with Insert, Update and Delete created correctly"); obj.responseText = "foo"; - obj.options = {}; t.delay_call(0.1, function() {obj.callback.call(this)}); return obj; }; @@ -248,7 +247,6 @@ 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; };