[OpenLayers-Users] Reusing Strategy objects

ol_in_nc winniethepujols at gmail.com
Fri Jul 27 17:23:44 PDT 2012


I believe it's working the way it's supposed to. 

If you try to use the same strategy for both layers, you're referencing the
same OpenLayers.Strategy.Filter object in memory. If you look through the
source of this class, you'll see properties like "layer" ('The layer this
strategy belongs to.'), so it definitely appears it was intended to be used
exclusively for one layer.

If that's a little unclear, think of it like this:

// add the first layer 
strat = new OpenLayers.Strategy.Filter({filter: filter});
layer1 = new OpenLayers.Layer.Vector("Layer1", {strategies: [strat]...});
strat['layer'] will now be a reference to layer1.

// add the second layer
layer2 = new OpenLayers.Layer.Vector("Layer2", {strategies: [strat]...});
strat['layer'] = ... already a reference to layer1, this isn't going to end
well 


It sounds like you've already found a solution and are just asking for an
explanation of why it doesn't work. If you haven't, try something like
this...


function shared_strategy() {
   return new OpenLayers.Strategy.Filter({
         filter: new OpenLayers.Filter.Comparison({...});
   });
}

layer1 = new OpenLayers.Layer.Vector("Layer1", {strategies: [strat1, strat2,
shared_strategy()]...});
layer2 = new OpenLayers.Layer.Vector("Layer2", {strategies: [strat3, strat4,
shared_strategy()]...}); 




--
View this message in context: http://osgeo-org.1560.n6.nabble.com/Reusing-Strategy-objects-tp4989163p4991475.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120727/59c1c670/attachment.html>


More information about the Users mailing list