[LayerGroup] Use eachLayer for iterations over layers (#5809)

* [LayerGroup] use eachLayer method instead of for...in

* [LayerGroup] use eachLayer method in getLayers method

* [LayerGroup] use eachLayer method in onAdd & onRemove methods

* [LayerGroup] invoke method fix

- use `eachLayer` method instead of for...in
- add unit test for `invoke` method

* revert invoke method change
This commit is contained in:
Andrew 2017-09-30 15:58:10 +02:00 committed by Vladimir Agafonkin
parent 7e49242aa2
commit 3e3f9e89c4
2 changed files with 20 additions and 14 deletions

View File

@ -92,4 +92,20 @@
L.geoJson(layerGroup.toGeoJSON()); L.geoJson(layerGroup.toGeoJSON());
}); });
}); });
describe("#invoke", function () {
it('should invoke `setOpacity` method on every layer', function () {
var layers = [
L.marker([0, 0]),
L.marker([1, 1])
];
var lg = L.layerGroup(layers);
var opacity = 0.5;
expect(layers[0].options.opacity).to.not.eql(opacity);
lg.invoke('setOpacity', opacity);
expect(layers[0].options.opacity).to.eql(opacity);
expect(layers[1].options.opacity).to.eql(opacity);
});
});
}); });

View File

@ -77,10 +77,7 @@ export var LayerGroup = Layer.extend({
// @method clearLayers(): this // @method clearLayers(): this
// Removes all the layers from the group. // Removes all the layers from the group.
clearLayers: function () { clearLayers: function () {
for (var i in this._layers) { return this.eachLayer(this.removeLayer, this);
this.removeLayer(this._layers[i]);
}
return this;
}, },
// @method invoke(methodName: String, …): this // @method invoke(methodName: String, …): this
@ -103,15 +100,11 @@ export var LayerGroup = Layer.extend({
}, },
onAdd: function (map) { onAdd: function (map) {
for (var i in this._layers) { this.eachLayer(map.addLayer, map);
map.addLayer(this._layers[i]);
}
}, },
onRemove: function (map) { onRemove: function (map) {
for (var i in this._layers) { this.eachLayer(map.removeLayer, map);
map.removeLayer(this._layers[i]);
}
}, },
// @method eachLayer(fn: Function, context?: Object): this // @method eachLayer(fn: Function, context?: Object): this
@ -138,10 +131,7 @@ export var LayerGroup = Layer.extend({
// Returns an array of all the layers added to the group. // Returns an array of all the layers added to the group.
getLayers: function () { getLayers: function () {
var layers = []; var layers = [];
this.eachLayer(layers.push, layers);
for (var i in this._layers) {
layers.push(this._layers[i]);
}
return layers; return layers;
}, },