[OpenLayers-Users] Understanding events

Eric Lemoine eric.lemoine at camptocamp.com
Wed Dec 9 10:43:29 EST 2009


On Wed, Dec 9, 2009 at 4:13 PM, Yves Moisan <yves.moisan at boreal-is.com> wrote:
>
>> Wrap your function in anonymous instead.
>
>> layers[0].events.register("loadend", layers[0], function(evt){
>>   //do what you have to do here...
>>    selectFeature(layers[0],oFeatures[0].fid));
>> });
>>
>
> Well, the use case is a bit more complicated.  I calculate initial
> bounds for a feature then select that feature.  Then if my feature array
> has more than one element I iterate through it and then features don't
> get selected :
>
> if(oFeatures && oFeatures.length > 0)
>
> {
>     var bounds = oFeatures[0].geometry.getBounds();
>        layers[0].events.register("loadend", layers[0], function(){
>        selectFeature(layers[0],oFeatures[0].fid);                                                                             });
>
>        for(var i = 1; i<oFeatures.length;++i)
>                      {
>                        bounds.extend(oFeatures[i].geometry.getBounds());
>
>                        layers[0].events.register("loadend", layers[0], function(){
>                           selectFeature(layers[0],oFeatures[i].fid);});
>
>
>
> I guess the event is triggered only once so iterating makes no sense ?
> I thought it was the case so I just called the function directly in the
> for like
>
> selectFeature(layers[0],oFeatures[i].fid);
>
> but that didn't work either probably because the code gets executed
> before the loadend was fired.  Do I have to set up an explicit delay
> here or somehow bubble the event ?

I'm not sure I get it. Why don't you do the whole thing once the layer
is loaded?

layer.events.on({
    scope: layer,
    loadend: function() {
        // "this" references the layer
        var features = this.features;
        // do what you need with the features
        // ...
    }
});

the loadend listener need not be anonymous, you can also go with:

function onLoadend() {
    var features = this.features;
}
layer.events.on({
    scope: layer,
    loadend: onLoadend // you don't call the function here but just
provide a reference to it
});

Hope this makes sense,

-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : eric.lemoine at camptocamp.com
http://www.camptocamp.com



More information about the Users mailing list