[OpenLayers-Dev] Event listeners and/or onXXX methods

Tim Schaub tschaub at opengeo.org
Fri Nov 5 17:03:27 EDT 2010


In general application developers should be registering event listeners. 
  The onFoo family of methods should be private.  Though ours aren't 
used this way currently, onFoo type methods are useful if you are 
developing a component that extends or wraps other components.

There can be many event listeners.  These are called in the order they 
registered.  There can be only one onFoo defintion.

One thing to discuss for the future is error handling in event 
listeners.  It is inconsistent with native event listener behavior that 
we stop calling listeners when one of them throws.  This makes plugin 
development riskier (your code breaks mine).

document.addEventListener("click", function() {oops}, true);
document.addEventListener("click", function() {alert("whew")}, true)
map.events.on({moveend: function() {oops}})
map.events.on({moveend: function() {alert("darn")}})

Two errors thrown, only "whew" alerted.

Different topic, I know.  My answer to event listeners versus onXXX 
methods: use event listeners if you want to play well with others (and 
we could use some example updates and code deprecation).

Tim

On 11/5/10 12:01 PM, Xurxo Mendez wrote:
> Hi all,
>
> This is a question I asked some times myself when using some OpenLayers
> features, and it raised again after reading Bart's post in GeoExt devs
> list (http://www.geoext.org/pipermail/dev/2010-November/000893.html).
> There are some classes that trigger some event (of some types) when some
> things occur (e.g. the SelectFeature control triggers, for instance,
> *featurehighlighted* event). Other (or same) classes provide the user
> with functions he can write and they will be executed also when some
> things occur (e.g. the same SelectFeature control, the *onSelect*
> function). So far I didn't find a duplicate use case, but I understand
> those are 2 different ways to do the same thing (maybe the order your
> code is executed is determined in a different way). Are they almost the
> same or is there a way preferred? Maybe, if that's the case, this could
> be reconsidered in OpenLayers 3
>
> Best regards,
>
> Xurxo Méndez Pérez
>
> http://www.sonxurxo.com <http://www.sonxurxo.com/>
>
>
>
> _______________________________________________
> Dev mailing list
> Dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-dev


-- 
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.


More information about the Dev mailing list