Commit a change to the Strategy/Fixed to behave more like GeoRSS/Text/GML

layers, and not load by default if the layer is not visible. Includes tests.
Default Behavior can be changed with 
'OpenLayers.Strategy.Fixed.prototype.preload=true;'. With review from elemoine,
comments from tschaub, review from me. Patch by Edgemaster. (Closes #1852) 


git-svn-id: http://svn.openlayers.org/trunk/openlayers@8775 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2009-01-28 01:43:04 +00:00
parent be82c1ef6e
commit 256f027c7c
2 changed files with 71 additions and 16 deletions
+32 -6
View File
@@ -15,6 +15,14 @@
*/ */
OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, { OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
/**
* APIProperty: preload
* {Boolean} Load data before layer made visible. Enabling this may result
* in considerable overhead if your application loads many data layers
* that are not visible by default. Default is true.
*/
preload: false,
/** /**
* Constructor: OpenLayers.Strategy.Fixed * Constructor: OpenLayers.Strategy.Fixed
* Create a new Fixed strategy. * Create a new Fixed strategy.
@@ -37,8 +45,7 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
/** /**
* Method: activate * Method: activate
* Activate the strategy: reads all features from the protocol and add them * Activate the strategy: load data or add listener to load when visible
* to the layer.
* *
* Returns: * Returns:
* {Boolean} True if the strategy was successfully activated or false if * {Boolean} True if the strategy was successfully activated or false if
@@ -46,15 +53,34 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
*/ */
activate: function() { activate: function() {
if(OpenLayers.Strategy.prototype.activate.apply(this, arguments)) { if(OpenLayers.Strategy.prototype.activate.apply(this, arguments)) {
this.layer.protocol.read({ if(this.layer.visibility == true || this.preload) {
callback: this.merge, this.load();
scope: this } else {
}); this.layer.events.on({
"visibilitychanged": this.load,
scope: this
});
}
return true; return true;
} }
return false; return false;
}, },
/**
* Method: load
* Tells protocol to load data and unhooks the visibilitychanged event
*/
load: function() {
this.layer.protocol.read({
callback: this.merge,
scope: this
});
this.layer.events.un({
"visibilitychanged": this.load,
scope: this
});
},
/** /**
* Method: merge * Method: merge
* Add all features to the layer. * Add all features to the layer.
+39 -10
View File
@@ -4,26 +4,55 @@
<script type="text/javascript"> <script type="text/javascript">
function test_activate(t) { function test_activate(t) {
t.plan(1); t.plan(5);
var featureList = ['foo', 'bar']; var featureList = ['foo', 'bar'];
// a fake protocol
var protocol = {
read: function(options) {
options.callback.call(options.scope, {features: featureList});
}
};
var layer = new OpenLayers.Layer.Vector("Vector Layer", { var layer = new OpenLayers.Layer.Vector("Vector Layer", {
strategies: [new OpenLayers.Strategy.Fixed()], strategies: [new OpenLayers.Strategy.Fixed()],
protocol: protocol,
// a fake protocol
protocol: {
read: function(options) {
options.callback.call(options.scope, {features: featureList});
}
},
addFeatures: function(features) { addFeatures: function(features) {
t.eq(features, featureList, "Features added to the layer"); t.eq(features, featureList, "Features added to the layer");
} }
}); });
var layerp = new OpenLayers.Layer.Vector("Hidden preload Layer", {
strategies: [new OpenLayers.Strategy.Fixed({preload:true})],
protocol: protocol,
visibility: false,
addFeatures: function(features) {
t.ok(!this.visibility, "Features preloaded before visible");
}
});
var s = new OpenLayers.Strategy.Fixed();
var layer2 = new OpenLayers.Layer.Vector("Hidden lazyload Layer", {
strategies: [s],
protocol: protocol,
visibility: false,
addFeatures: function(features) {
t.ok(this.visibility, "Layer visible when features added");
}
});
var map = new OpenLayers.Map('map'); var map = new OpenLayers.Map('map');
map.addLayer(layer); map.addLayers([layer, layerp, layer2]);
t.ok(layer2.events.listeners["visibilitychanged"][0].obj == s &&
layer2.events.listeners["visibilitychanged"][0].func == s.load,
"activate registers visibilitychanged listener if layer hidden"+
" and is lazyloading");
layer2.setVisibility(true);
t.ok(layer2.events.listeners["visibilitychanged"] == false,
"visibilitychanged listener unregistered");
} }
</script> </script>