Evented
Quick answer
L.Evented is the event base class shared by maps, layers and controls, and you can instantiate it directly as a standalone event bus. It provides on, off, once and fire with optional bubbling through registered event parents. Maps, layers and popups all use it for their click and lifecycle notifications.
L.Evented. extend ( props ) → Evented 子类
Key points
- fire takes a third argument that enables propagation upward to event parents.
- addEventParent makes custom objects bubble their own events to the map.
- Listeners run in registration order, and a throwing callback affects later ones.
Example
var Selection = L.Evented.extend({
select: function (id) {
this.selectedId = id;
this.fire('select', { id: id });
}
});
var selection = new Selection();
selection.on('select', function (e) {
console.log('选中', e.id, '来自', e.target === selection);
});
selection.once('select', function () {
console.log('只执行一次');
});
selection.select(12);
selection.select(13);var layerBus = new L.Evented();
layerBus.addEventParent(map);
layerBus.fire('status', { ready: true }, true);
map.on('status', function (e) {
console.log('收到子级事件', e.ready);
});
Notes
- off with no arguments clears every listener, including ones registered elsewhere on a shared bus.
- Anonymous callbacks cannot be unbound individually, so component teardown leaks closures.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Evented. https://www.jingweizhi.com/en/leaflet/api/evented.html (accessed 2026-11-03).