Fixed the scope of onSelect, onUnselect,... when scope is not specified.

r=crschmidt (pullup #2115)


git-svn-id: http://svn.openlayers.org/trunk/openlayers@9419 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2009-06-01 16:27:02 +00:00
parent 553f05d293
commit d6a3c508a1
2 changed files with 10 additions and 4 deletions

View File

@@ -111,7 +111,7 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
* {Object} The scope to use with the onBeforeSelect, onSelect, onUnselect * {Object} The scope to use with the onBeforeSelect, onSelect, onUnselect
* callbacks. If null the scope will be this control. * callbacks. If null the scope will be this control.
*/ */
scope: this, scope: null,
/** /**
* APIProperty: geometryTypes * APIProperty: geometryTypes
@@ -178,6 +178,10 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
OpenLayers.Control.prototype.EVENT_TYPES OpenLayers.Control.prototype.EVENT_TYPES
); );
OpenLayers.Control.prototype.initialize.apply(this, [options]); OpenLayers.Control.prototype.initialize.apply(this, [options]);
if(this.scope === null) {
this.scope = this;
}
if(layers instanceof Array) { if(layers instanceof Array) {
this.layers = layers; this.layers = layers;
this.layer = new OpenLayers.Layer.Vector.RootContainer( this.layer = new OpenLayers.Layer.Vector.RootContainer(

View File

@@ -68,7 +68,7 @@
} }
function test_Control_SelectFeature_clickFeature(t) { function test_Control_SelectFeature_clickFeature(t) {
t.plan(4); t.plan(6);
// mock up layer // mock up layer
var layer = { var layer = {
selectedFeatures: [], selectedFeatures: [],
@@ -100,6 +100,7 @@
"onSelect called with proper feature (" + feature.index + ")"); "onSelect called with proper feature (" + feature.index + ")");
t.eq(feature.tested, feature.test, t.eq(feature.tested, feature.test,
"onSelect called only once for feature (" + feature.index + ")"); "onSelect called only once for feature (" + feature.index + ")");
t.ok(this == control, "onSelect called in the scope of the control if control.scope is not provided");
} }
// test that onUnselect gets called properly // test that onUnselect gets called properly
@@ -109,9 +110,10 @@
"onUnselect called with proper feature (" + feature.index + ")"); "onUnselect called with proper feature (" + feature.index + ")");
t.eq(feature.tested, feature.test, t.eq(feature.tested, feature.test,
"onUnselect called only once for feature (" + feature.index + ")"); "onUnselect called only once for feature (" + feature.index + ")");
t.ok(this == control, "onUnselect called in the scope of the control if control.scope is not provided");
} }
// mock up first click on first feature (runs 2 tests from onSelect) // mock up first click on first feature (runs 3 tests from onSelect)
var feature = features[0]; var feature = features[0];
feature.index = 0; feature.index = 0;
feature.test = 1; feature.test = 1;
@@ -121,7 +123,7 @@
control.toggle = false; control.toggle = false;
control.clickFeature(feature); control.clickFeature(feature);
// mock up second click on first feature (runs 2 tests from onUnselect) // mock up second click on first feature (runs 3 tests from onUnselect)
control.toggle = true; control.toggle = true;
feature.test = 2; feature.test = 2;
control.clickFeature(feature); control.clickFeature(feature);