From f9ae12f2271adf4422da33f4dc9797d61d24e89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Wed, 19 Oct 2011 20:42:34 +0200 Subject: [PATCH] minor refactoring in Layer.Grid - add a removeBackBuffer function --- lib/OpenLayers/Layer/Grid.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/OpenLayers/Layer/Grid.js b/lib/OpenLayers/Layer/Grid.js index fd13b1aabb..fc2232a6a8 100644 --- a/lib/OpenLayers/Layer/Grid.js +++ b/lib/OpenLayers/Layer/Grid.js @@ -462,7 +462,7 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, { return; } // to be able to correctly position the back buffer we - // place the first tile at (0, 0) in the back buffer + // place the tiles grid at (0, 0) in the back buffer tile.style.left = (j * this.tileSize.w) + '%'; tile.style.top = (i * this.tileSize.h) + '%'; backBuffer.appendChild(tile); @@ -472,15 +472,31 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, { return backBuffer; }, + /** + * Method: removeBackBuffer + * Remove back buffer from DOM. + */ + removeBackBuffer: function() { + if(this.backBuffer && this.backBuffer.parentNode) { + this.div.removeChild(this.backBuffer); + this.backBuffer = null; + } + }, + /** * Method: updateBackBufferData * Upstate states in the backBufferData property */ updateBackBufferData: function() { - this.backBufferData.resolution = this.getServerResolution(); - var topLeftTile = this.grid[0][0]; - this.backBufferData.lonlat = {lon: topLeftTile.bounds.left, - lat: topLeftTile.bounds.top}; + // updateBackBufferData is called asynchronously when tiles are + // received, so we need to check that the map is still there + if (this.map) { + var resolution = this.map.getResolution(); + this.backBufferData.resolution = this.getServerResolution(resolution); + var topLeftTile = this.grid[0][0]; + this.backBufferData.lonlat = {lon: topLeftTile.bounds.left, + lat: topLeftTile.bounds.top}; + } }, /** @@ -902,13 +918,8 @@ OpenLayers.Layer.Grid = OpenLayers.Class(OpenLayers.Layer.HTTPRequest, { //if that was the last tile, then trigger a 'loadend' on the layer if (this.numLoadingTiles == 0) { this.events.triggerEvent("loadend"); - if(this.backBuffer && this.backBuffer.parentNode) { - this.div.removeChild(this.backBuffer); - this.backBuffer = null; - } - if(this.map) { - this.updateBackBufferData(); - } + this.removeBackBuffer(); + this.updateBackBufferData(); } }; tile.events.register("loadend", this, tile.onLoadEnd);