Add Map#eachLayer (fixes #1457)

This commit is contained in:
John Firebaugh 2013-02-26 11:39:00 -08:00
parent bee90ce0e5
commit 511fbb9f44
2 changed files with 34 additions and 0 deletions

View File

@ -180,4 +180,29 @@ describe("Map", function () {
});
});
});
describe("#eachLayer", function () {
it("returns self", function () {
expect(map.eachLayer(function () {})).toBe(map);
});
it("calls the provided function for each layer", function () {
var t1 = L.tileLayer("{z}{x}{y}").addTo(map),
t2 = L.tileLayer("{z}{x}{y}").addTo(map);
map.eachLayer(spy);
expect(spy.calls.length).toEqual(2);
expect(spy.calls[0].args).toEqual([t1]);
expect(spy.calls[1].args).toEqual([t2]);
});
it("calls the provided function with the provided context", function () {
var t1 = L.tileLayer("{z}{x}{y}").addTo(map);
map.eachLayer(spy, map);
expect(spy.calls[0].object).toEqual(map);
});
});
});

View File

@ -198,6 +198,15 @@ L.Map = L.Class.extend({
return this._layers.hasOwnProperty(id);
},
eachLayer: function (method, context) {
for (var i in this._layers) {
if (this._layers.hasOwnProperty(i)) {
method.call(context, this._layers[i]);
}
}
return this;
},
invalidateSize: function (animate) {
var oldSize = this.getSize();