From 0b4d2b0c4426cfce6c71632c0f27e2aa3fd9a6de Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 12 Oct 2012 12:49:12 -0700 Subject: [PATCH] Add Map#whenReady Fixes #1063 --- spec/runner.html | 1 + spec/suites/control/Control.ScaleSpec.js | 6 ++++++ spec/suites/map/MapSpec.js | 27 ++++++++++++++++++++++++ src/control/Control.Scale.js | 2 +- src/map/Map.js | 19 ++++++++++------- 5 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 spec/suites/control/Control.ScaleSpec.js diff --git a/spec/runner.html b/spec/runner.html index 6d60c2e3..41f7c9ff 100644 --- a/spec/runner.html +++ b/spec/runner.html @@ -23,6 +23,7 @@ + diff --git a/spec/suites/control/Control.ScaleSpec.js b/spec/suites/control/Control.ScaleSpec.js new file mode 100644 index 00000000..55aa4d5f --- /dev/null +++ b/spec/suites/control/Control.ScaleSpec.js @@ -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); + }) +}); diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index 79abaaae..f962dd1a 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -1,4 +1,31 @@ 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 () { it("is safe to call from within a moveend callback during initial load (#1027)", function () { var c = document.createElement('div'), diff --git a/src/control/Control.Scale.js b/src/control/Control.Scale.js index 5b946b8d..036031ce 100644 --- a/src/control/Control.Scale.js +++ b/src/control/Control.Scale.js @@ -17,7 +17,7 @@ L.Control.Scale = L.Control.extend({ this._addScales(options, className, container); map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this); - this._update(); + map.whenReady(this._update, this); return container; }, diff --git a/src/map/Map.js b/src/map/Map.js index 5a33fcee..39789595 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -162,16 +162,10 @@ L.Map = L.Class.extend({ layer.on('load', this._onTileLayerLoad, this); } - var onMapLoad = function () { + this.whenReady(function () { layer.onAdd(this); this.fire('layeradd', {layer: layer}); - }; - - if (this._loaded) { - onMapLoad.call(this); - } else { - this.on('load', onMapLoad, this); - } + }, 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