Add Map#whenReady

Fixes #1063
This commit is contained in:
John Firebaugh 2012-10-12 12:49:12 -07:00
parent 2f6bd65efd
commit 0b4d2b0c44
5 changed files with 46 additions and 9 deletions

View File

@ -23,6 +23,7 @@
<!-- /control --> <!-- /control -->
<script type="text/javascript" src="suites/control/Control.LayersSpec.js"></script> <script type="text/javascript" src="suites/control/Control.LayersSpec.js"></script>
<script type="text/javascript" src="suites/control/Control.ScaleSpec.js"></script>
<!-- /core --> <!-- /core -->
<script type="text/javascript" src="suites/core/UtilSpec.js"></script> <script type="text/javascript" src="suites/core/UtilSpec.js"></script>

View File

@ -0,0 +1,6 @@
describe("Control.Scale", function () {
it("can be added to an unloaded map", function () {
var map = L.map(document.createElement('div'));
new L.Control.Scale().addTo(map);
})
});

View File

@ -1,4 +1,31 @@
describe("Map", function () { describe("Map", function () {
describe("#whenReady", function () {
describe("when the map has not yet been loaded", function () {
it("calls the callback when the map is loaded", function () {
var map = L.map(document.createElement('div')),
spy = jasmine.createSpy();
map.whenReady(spy);
expect(spy).not.toHaveBeenCalled();
map.setView([0, 0], 1);
expect(spy).toHaveBeenCalled();
})
});
describe("when the map has already been loaded", function () {
it("calls the callback immediately", function () {
var map = L.map(document.createElement('div')),
spy = jasmine.createSpy();
map.setView([0, 0], 1);
map.whenReady(spy);
expect(spy).toHaveBeenCalled();
});
});
});
describe("#getBounds", function () { describe("#getBounds", function () {
it("is safe to call from within a moveend callback during initial load (#1027)", function () { it("is safe to call from within a moveend callback during initial load (#1027)", function () {
var c = document.createElement('div'), var c = document.createElement('div'),

View File

@ -17,7 +17,7 @@ L.Control.Scale = L.Control.extend({
this._addScales(options, className, container); this._addScales(options, className, container);
map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this); map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
this._update(); map.whenReady(this._update, this);
return container; return container;
}, },

View File

@ -162,16 +162,10 @@ L.Map = L.Class.extend({
layer.on('load', this._onTileLayerLoad, this); layer.on('load', this._onTileLayerLoad, this);
} }
var onMapLoad = function () { this.whenReady(function () {
layer.onAdd(this); layer.onAdd(this);
this.fire('layeradd', {layer: layer}); this.fire('layeradd', {layer: layer});
}; }, this);
if (this._loaded) {
onMapLoad.call(this);
} else {
this.on('load', onMapLoad, this);
}
return this; return this;
}, },
@ -606,6 +600,15 @@ L.Map = L.Class.extend({
} }
}, },
whenReady: function (callback, context) {
if (this._loaded) {
callback.call(context || this, this);
} else {
this.on('load', callback, context);
}
return this;
},
// private methods for getting map state // private methods for getting map state