From 837efd0b9c77a153f9a3b7dfb37787a41181290e Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 9 Mar 2009 22:17:23 +0000 Subject: [PATCH] Loading strategies now trigger loadstart and loadend events. r=crschmidt (closes #1840) git-svn-id: http://svn.openlayers.org/trunk/openlayers@8973 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Strategy/BBOX.js | 2 + lib/OpenLayers/Strategy/Fixed.js | 2 + tests/Strategy/BBOX.html | 70 +++++++++++++++++++++++++++----- tests/Strategy/Fixed.html | 57 ++++++++++++++++++++++++-- 4 files changed, 117 insertions(+), 14 deletions(-) diff --git a/lib/OpenLayers/Strategy/BBOX.js b/lib/OpenLayers/Strategy/BBOX.js index a5a855b98d..a513a34ddf 100644 --- a/lib/OpenLayers/Strategy/BBOX.js +++ b/lib/OpenLayers/Strategy/BBOX.js @@ -176,6 +176,7 @@ OpenLayers.Strategy.BBOX = OpenLayers.Class(OpenLayers.Strategy, { */ triggerRead: function() { this.layer.protocol.abort(this.response); + this.layer.events.triggerEvent("loadstart"); this.response = this.layer.protocol.read({ filter: this.createFilter(), callback: this.merge, @@ -231,6 +232,7 @@ OpenLayers.Strategy.BBOX = OpenLayers.Class(OpenLayers.Strategy, { } this.layer.addFeatures(features); } + this.layer.events.triggerEvent("loadend"); }, CLASS_NAME: "OpenLayers.Strategy.BBOX" diff --git a/lib/OpenLayers/Strategy/Fixed.js b/lib/OpenLayers/Strategy/Fixed.js index 217bef2f20..3885d4bf69 100644 --- a/lib/OpenLayers/Strategy/Fixed.js +++ b/lib/OpenLayers/Strategy/Fixed.js @@ -71,6 +71,7 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, { * Tells protocol to load data and unhooks the visibilitychanged event */ load: function() { + this.layer.events.triggerEvent("loadstart"); this.layer.protocol.read({ callback: this.merge, scope: this @@ -101,6 +102,7 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, { } this.layer.addFeatures(features); } + this.layer.events.triggerEvent("loadend"); }, CLASS_NAME: "OpenLayers.Strategy.Fixed" diff --git a/tests/Strategy/BBOX.html b/tests/Strategy/BBOX.html index 49fb115a77..6352d8bee6 100644 --- a/tests/Strategy/BBOX.html +++ b/tests/Strategy/BBOX.html @@ -83,6 +83,42 @@ t.ok(strategy.bounds.equals(map.getExtent().transform(from, to)), "[force update different proj] bounds transformed"); + } + + function test_events(t) { + + t.plan(2); + var log = { + loadstart: 0, + loadend: 0 + }; + + var map = new OpenLayers.Map("map"); + var layer = new OpenLayers.Layer.Vector(null, { + strategies: [new OpenLayers.Strategy.BBOX()], + protocol: new OpenLayers.Protocol({ + read: function(config) { + config.callback.call(config.scope, {}); + } + }), + isBaseLayer: true, + eventListeners: { + loadstart: function() { + ++log.loadstart; + }, + loadend: function() { + ++log.loadend; + } + } + }); + map.addLayer(layer); + map.zoomToMaxExtent(); + + t.eq(log.loadstart, 1, "loadstart triggered"); + t.eq(log.loadend, 1, "loadend triggered"); + + map.destroy(); + } function test_triggerRead(t) { @@ -96,26 +132,38 @@ return filter; }; s.response = {"fake": "response"}; + + var log = {}; var protocol = new OpenLayers.Protocol({ read: function(options) { - t.ok(options.filter == filter, - "protocol read called with correct filter"); - t.ok(options.callback == s.merge, - "protocol read called with correct callback"); - t.ok(options.scope == s, - "protocol read called with correct scope"); + log.options = options; }, abort: function(response) { - t.eq(response, s.response, - "protocol abort called with correct response"); + log.abort = response.fake; } }); + + var layer = new OpenLayers.Layer.Vector(null, { + strategies: [s], + protocol: protocol, + isBaseLayer: true + }); + var map = new OpenLayers.Map("map"); + map.addLayer(layer); + map.zoomToMaxExtent(); + + t.ok(log.options.filter == filter, + "protocol read called with correct filter"); + t.ok(log.options.callback == s.merge, + "protocol read called with correct callback"); + t.ok(log.options.scope == s, + "protocol read called with correct scope"); + t.eq(log.abort, "response", + "protocol abort called with correct response"); - s.setLayer({protocol: protocol}); + map.destroy(); - // 4 tests - s.triggerRead(); } function test_createFilter(t) { diff --git a/tests/Strategy/Fixed.html b/tests/Strategy/Fixed.html index 0bb8c7f90e..bf03d9e336 100644 --- a/tests/Strategy/Fixed.html +++ b/tests/Strategy/Fixed.html @@ -60,21 +60,69 @@ "visibilitychanged listener unregistered"); } - function tests_merge(t) { + function test_events(t) { - t.plan(4); + t.plan(2); + + var log = { + loadstart: 0, + loadend: 0 + }; + + var map = new OpenLayers.Map("map"); + var layer = new OpenLayers.Layer.Vector(null, { + strategies: [new OpenLayers.Strategy.Fixed()], + protocol: new OpenLayers.Protocol({ + read: function(config) { + config.callback.call(config.scope, {}); + } + }), + isBaseLayer: true, + eventListeners: { + loadstart: function() { + ++log.loadstart; + }, + loadend: function() { + ++log.loadend; + } + } + }); + + map.addLayer(layer); + map.zoomToMaxExtent(); + + t.eq(log.loadstart, 1, "loadstart triggered"); + t.eq(log.loadend, 1, "loadend triggered"); + + map.destroy(); + + } + + + function test_merge(t) { + + t.plan(5); var strategy = new OpenLayers.Strategy.Fixed(); // create map with default projection var map = new OpenLayers.Map("map"); + var log = { + loadend: 0 + }; + // create layer with custom projection var layer = new OpenLayers.Layer.Vector(null, { isBaseLayer: true, strategies: [strategy], protocol: new OpenLayers.Protocol(), - projection: new OpenLayers.Projection("EPSG:900913") + projection: new OpenLayers.Projection("EPSG:900913"), + eventListeners: { + loadend: function() { + ++log.loadend; + } + } }); map.addLayer(layer); map.zoomToMaxExtent(); @@ -92,6 +140,9 @@ // call merge with a mocked up response strategy.merge({features: features}); + // confirm that loadend was called + t.eq(log.loadend, 1, "merge triggers loadend"); + // test that feature geometries have been transformed to map projection var from = layer.projection; var to = map.getProjectionObject();