we get a js error in the Fixed strategy when the layer is removed from the map right after strategy.load is called, p=fvanderbiest, r=me (closes #2851)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10791 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2010-09-27 10:13:00 +00:00
parent 0dc5793834
commit 059b83a52f
2 changed files with 70 additions and 16 deletions

View File

@@ -98,13 +98,14 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
* options - {Object} options to pass to protocol read.
*/
load: function(options) {
this.layer.events.triggerEvent("loadstart");
this.layer.protocol.read(OpenLayers.Util.applyDefaults({
callback: this.merge,
filter: this.layer.filter,
scope: this
var layer = this.layer;
layer.events.triggerEvent("loadstart");
layer.protocol.read(OpenLayers.Util.applyDefaults({
callback: OpenLayers.Function.bind(this.merge, this,
layer.map.getProjectionObject()),
filter: layer.filter
}, options));
this.layer.events.un({
layer.events.un({
"visibilitychanged": this.load,
scope: this
});
@@ -113,25 +114,28 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
/**
* Method: merge
* Add all features to the layer.
*
* Parameters:
* mapProjection - {OpenLayers.Projection} the map projection
* resp - {Object} options to pass to protocol read.
*/
merge: function(resp) {
this.layer.destroyFeatures();
merge: function(mapProjection, resp) {
var layer = this.layer;
layer.destroyFeatures();
var features = resp.features;
if (features && features.length > 0) {
var remote = this.layer.projection;
var local = this.layer.map.getProjectionObject();
if(!local.equals(remote)) {
if(!mapProjection.equals(layer.projection)) {
var geom;
for(var i=0, len=features.length; i<len; ++i) {
geom = features[i].geometry;
if(geom) {
geom.transform(remote, local);
geom.transform(layer.projection, mapProjection);
}
}
}
this.layer.addFeatures(features);
layer.addFeatures(features);
}
this.layer.events.triggerEvent("loadend");
layer.events.triggerEvent("loadend");
},
CLASS_NAME: "OpenLayers.Strategy.Fixed"

View File

@@ -153,7 +153,7 @@
];
// call merge with a mocked up response
strategy.merge({features: features});
strategy.merge(new OpenLayers.Projection("EPSG:900913"), {features: features});
// confirm that the original features were destroyed
t.eq(layer.features.length, 2, "old features destroyed");
@@ -177,7 +177,7 @@
];
// call merge again with mocked up response
strategy.merge({features: features});
strategy.merge(new OpenLayers.Projection("EPSG:900913"), {features: features});
// test that feature geometries have not been transformed
t.geom_eq(layer.features[0].geometry, features[0].geometry, "[same proj] feature 0 geometry not transformed");
@@ -185,6 +185,56 @@
}
function test_load(t) {
t.plan(4);
// set up
var log;
var map = new OpenLayers.Map({
div: "map",
projection: new OpenLayers.Projection("EPSG:900913"),
layers: [new OpenLayers.Layer("", {isBaseLayer: true})]
});
var response = new OpenLayers.Protocol.Response();
var strategy = new OpenLayers.Strategy.Fixed({
merge: function(p, r) {
log = {scope: this, projection: p, response: r};
}
});
var layer = new OpenLayers.Layer.Vector("vector", {
strategies: [strategy],
protocol: {
read: function(o) {
o.callback.call(o.scope, response);
}
}
});
map.addLayer(layer);
// test
strategy.load();
// verify that the callback is correctly bound
t.ok(log !== undefined,
"merge was called");
t.ok(log.scope == strategy,
"merge called with expected scope");
t.eq(log.projection.getCode(), map.getProjectionObject().getCode(),
"merge called the map projection as the first arg");
t.ok(log.response == response,
"merge called with response as the first arg");
// tear down
map.destroy();
}
</script>
</head>
<body>