diff --git a/spec/suites/layer/vector/PathSpec.js b/spec/suites/layer/vector/PathSpec.js index 58763e90..fca44980 100644 --- a/spec/suites/layer/vector/PathSpec.js +++ b/spec/suites/layer/vector/PathSpec.js @@ -52,5 +52,30 @@ describe('Path', function () { expect(mapSpy.called).to.be.ok(); }); + it('can add a layer while being inside a moveend handler', function (done) { + var zoneLayer = L.layerGroup(); + var polygon; + map.addLayer(zoneLayer); + + map.on('moveend', function () { + zoneLayer.clearLayers(); + polygon = new L.Polygon([[1, 2], [3, 4], [5, 6]]); + zoneLayer.addLayer(polygon); + }); + + map.invalidateSize(); + map.setView([1, 2], 12, {animate: false}); + + map.panBy([-260, 0]); + setTimeout(function () { + expect(polygon._parts.length).to.be(0); + map.panBy([260, 0]); + setTimeout(function () { + expect(polygon._parts.length).to.be(1); + done(); + }, 300); + }, 300); + }); + }); }); diff --git a/src/layer/vector/Path.js b/src/layer/vector/Path.js index 1e03cd70..9b10e852 100644 --- a/src/layer/vector/Path.js +++ b/src/layer/vector/Path.js @@ -76,16 +76,17 @@ L.Path = L.Layer.extend({ this._renderer._initPath(this); this._reset(); this._renderer._addPath(this); + this._renderer.on('update', this._update, this); }, onRemove: function () { this._renderer._removePath(this); + this._renderer.off('update', this._update, this); }, getEvents: function () { return { zoomend: this._project, - moveend: this._update, viewreset: this._reset }; }, diff --git a/src/layer/vector/Renderer.js b/src/layer/vector/Renderer.js index 5dd4810c..24b4c06d 100644 --- a/src/layer/vector/Renderer.js +++ b/src/layer/vector/Renderer.js @@ -13,6 +13,9 @@ * * Do not use this class directly, use `SVG` and `Canvas` instead. * + * @event update: Event + * Fired when the renderer updates its bounds, center and zoom, for example when + * its map has moved */ L.Renderer = L.Layer.extend({ @@ -100,6 +103,8 @@ L.Renderer = L.Layer.extend({ this._center = this._map.getCenter(); this._zoom = this._map.getZoom(); + + this.fire('update'); } });