Move detection of map procjection to merge function, check for successful response and fix tests.

This commit is contained in:
friedjoff
2013-01-08 14:49:42 +01:00
parent bbc73a21d8
commit 88e184e7e5
2 changed files with 37 additions and 33 deletions
+28 -25
View File
@@ -1,4 +1,4 @@
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for /* Copyright (c) 2006-2012 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the 2-clause BSD license. * full list of contributors). Published under the 2-clause BSD license.
* See license.txt in the OpenLayers distribution or repository for the * See license.txt in the OpenLayers distribution or repository for the
* full text of the license. */ * full text of the license. */
@@ -42,7 +42,8 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
* the strategy was already active. * the strategy was already active.
*/ */
activate: function() { activate: function() {
if(OpenLayers.Strategy.prototype.activate.apply(this, arguments)) { var activated = OpenLayers.Strategy.prototype.activate.call(this);
if(activated) {
this.layer.events.on({ this.layer.events.on({
"refresh": this.load, "refresh": this.load,
scope: this scope: this
@@ -55,9 +56,8 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
scope: this scope: this
}); });
} }
return true;
} }
return false; return activated;
}, },
/** /**
@@ -87,14 +87,13 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
* options - {Object} options to pass to protocol read. * options - {Object} options to pass to protocol read.
*/ */
load: function(options) { load: function(options) {
var layer = this.layer; this.layer.events.triggerEvent("loadstart", {filter: this.layer.filter});
layer.events.triggerEvent("loadstart", {filter: layer.filter}); this.layer.protocol.read(OpenLayers.Util.applyDefaults({
layer.protocol.read(OpenLayers.Util.applyDefaults({ callback: this.merge,
callback: OpenLayers.Function.bind(this.merge, this, filter: this.layer.filter,
layer.map.getProjectionObject()), scope: this
filter: layer.filter
}, options)); }, options));
layer.events.un({ this.layer.events.un({
"visibilitychanged": this.load, "visibilitychanged": this.load,
scope: this scope: this
}); });
@@ -103,29 +102,33 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
/** /**
* Method: merge * Method: merge
* Add all features to the layer. * Add all features to the layer.
* If the layer projection differs from the map projection, features
* will be transformed from the layer projection to the map projection.
* *
* Parameters: * Parameters:
* mapProjection - {<OpenLayers.Projection>} the map projection
* resp - {<OpenLayers.Protocol.Response>} The response object passed * resp - {<OpenLayers.Protocol.Response>} The response object passed
* by the protocol. * by the protocol.
*/ */
merge: function(mapProjection, resp) { merge: function(resp) {
var layer = this.layer; this.layer.destroyFeatures();
layer.destroyFeatures(); if (resp.success()) {
var features = resp.features; var features = resp.features;
if (features && features.length > 0) { if (features && features.length > 0) {
if(!mapProjection.equals(layer.projection)) { var remote = this.layer.projection;
var geom; var local = this.layer.map.getProjectionObject();
for(var i=0, len=features.length; i<len; ++i) { if(!local.equals(remote)) {
geom = features[i].geometry; var geom;
if(geom) { for(var i=0, len=features.length; i<len; ++i) {
geom.transform(layer.projection, mapProjection); geom = features[i].geometry;
if(geom) {
geom.transform(remote, local);
}
} }
} }
this.layer.addFeatures(features);
} }
layer.addFeatures(features);
} }
layer.events.triggerEvent("loadend", {response: resp}); this.layer.events.triggerEvent("loadend", {response: resp});
}, },
CLASS_NAME: "OpenLayers.Strategy.Fixed" CLASS_NAME: "OpenLayers.Strategy.Fixed"
+9 -8
View File
@@ -10,7 +10,10 @@
// a fake protocol // a fake protocol
var protocol = { var protocol = {
read: function(options) { read: function(options) {
options.callback.call(options.scope, {features: featureList}); options.callback.call(options.scope, {
features: featureList,
success: OpenLayers.Function.True
});
} }
}; };
@@ -162,7 +165,7 @@
]; ];
// call merge with a mocked up response // call merge with a mocked up response
strategy.merge(new OpenLayers.Projection("EPSG:900913"), {features: features}); strategy.merge({features: features, success: OpenLayers.Function.True});
// confirm that the original features were destroyed // confirm that the original features were destroyed
t.eq(layer.features.length, 2, "old features destroyed"); t.eq(layer.features.length, 2, "old features destroyed");
@@ -186,7 +189,7 @@
]; ];
// call merge again with mocked up response // call merge again with mocked up response
strategy.merge(new OpenLayers.Projection("EPSG:900913"), {features: features}); strategy.merge({features: features, success: OpenLayers.Function.True});
// test that feature geometries have not been transformed // 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"); t.geom_eq(layer.features[0].geometry, features[0].geometry, "[same proj] feature 0 geometry not transformed");
@@ -195,7 +198,7 @@
} }
function test_load(t) { function test_load(t) {
t.plan(4); t.plan(3);
// set up // set up
@@ -210,8 +213,8 @@
var response = new OpenLayers.Protocol.Response(); var response = new OpenLayers.Protocol.Response();
var strategy = new OpenLayers.Strategy.Fixed({ var strategy = new OpenLayers.Strategy.Fixed({
merge: function(p, r) { merge: function(r) {
log = {scope: this, projection: p, response: r}; log = {scope: this, response: r};
} }
}); });
@@ -235,8 +238,6 @@
"merge was called"); "merge was called");
t.ok(log.scope == strategy, t.ok(log.scope == strategy,
"merge called with expected scope"); "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, t.ok(log.response == response,
"merge called with response as the first arg"); "merge called with response as the first arg");