Skip to content

Commit 26111d0

Browse files
committed
Adding refresh event and making TileManager listen for it
When calling redraw(true) on a HTTPRequest layer, mergeNewParams is used to add a url parameter with a random value. For layer types that do not use params in the getURL function, the url does not change and the TileManager will provide tiles from the cache. To make sure that tiles are fetched from the server, they need to be removed from the cache first.
1 parent b38d223 commit 26111d0

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

lib/OpenLayers/Layer/HTTPRequest.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
*/
1717
OpenLayers.Layer.HTTPRequest = OpenLayers.Class(OpenLayers.Layer, {
1818

19+
/**
20+
* APIProperty: events
21+
* {<OpenLayers.Events>}
22+
*
23+
* Supported event types (in addition to those from <OpenLayers.Layer.events>):
24+
* refresh - Triggered when a redraw is forced, to re-fetch data from the
25+
* server.
26+
*/
27+
1928
/**
2029
* Constant: URL_HASH_FACTOR
2130
* {Float} Used to hash URL param strings for multi-WMS server selection.
@@ -144,6 +153,7 @@ OpenLayers.Layer.HTTPRequest = OpenLayers.Class(OpenLayers.Layer, {
144153
*/
145154
redraw: function(force) {
146155
if (force) {
156+
this.events.triggerEvent('refresh');
147157
return this.mergeNewParams({"_olSalt": Math.random()});
148158
} else {
149159
return OpenLayers.Layer.prototype.redraw.apply(this, []);

lib/OpenLayers/TileManager.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ OpenLayers.TileManager = OpenLayers.Class({
215215
if (layer instanceof OpenLayers.Layer.Grid) {
216216
layer.events.on({
217217
addtile: this.addTile,
218+
refresh: this.refresh,
218219
retile: this.clearTileQueue,
219220
scope: this
220221
});
@@ -245,6 +246,7 @@ OpenLayers.TileManager = OpenLayers.Class({
245246
if (layer.events) {
246247
layer.events.un({
247248
addtile: this.addTile,
249+
refresh: this.refresh,
248250
retile: this.clearTileQueue,
249251
scope: this
250252
});
@@ -260,6 +262,27 @@ OpenLayers.TileManager = OpenLayers.Class({
260262
}
261263
}
262264
},
265+
266+
/**
267+
* Method: refresh
268+
* Clears the cache when a redraw is forced on a layer
269+
*
270+
* Parameters:
271+
* evt - {Object} The listener argument
272+
*/
273+
refresh: function(evt) {
274+
var layer = evt.object;
275+
if (layer.grid) {
276+
var i, j, tile;
277+
for (i=layer.grid.length-1; i>=0; --i) {
278+
for (j=layer.grid[i].length-1; j>=0; --j) {
279+
tile = layer.grid[i][j];
280+
OpenLayers.Util.removeItem(this.tileCacheIndex, tile.url);
281+
delete this.tileCache[tile.url];
282+
}
283+
}
284+
}
285+
},
263286

264287
/**
265288
* Method: updateTimeout

tests/TileManager.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,27 @@
129129
t.eq(tileManager.tileQueue[map.id].length, 0, "tiles loaded after moveDelay");
130130
});
131131
}
132+
133+
function test_refresh(t) {
134+
t.plan(1);
135+
var map = new OpenLayers.Map( 'map', {maxResolution: 1.40625/2} );
136+
var layer = new OpenLayers.Layer.TMS( "TMS",
137+
"../img/blank.gif/", {layername: 'basic', type:'png'} );
138+
map.addLayer(layer);
139+
map.setCenter(new OpenLayers.LonLat(5, 40), 5);
140+
var log = 0;
141+
t.delay_call(1, function() {
142+
var origSetImgSrc = OpenLayers.Tile.Image.prototype.setImgSrc;
143+
OpenLayers.Tile.Image.prototype.setImgSrc = function() {
144+
++log;
145+
OpenLayers.Tile.Image.prototype.setImgSrc = origSetImgSrc;
146+
};
147+
layer.redraw(true);
148+
});
149+
t.delay_call(1.5, function() {
150+
t.ok(log > 0, 'setImgSrc called to refresh tile.');
151+
});
152+
}
132153
</script>
133154
</head>
134155
<body>

0 commit comments

Comments
 (0)