Matt Constantine
2009-Jun-02 15:35 UTC
[Mapstraction] Deferring api calls until after a map has loaded
Hi Everyone, I''m a developer at ForitusOne. I''ve been working on the GeoCommons implementation for Mapstraction v2. Andrew Turner told me that there''s been some talk lately about whether the checkLoad functionality needs to be there for mxn v2. I wanted to chime in about it and present some code that demonstrates a lightweight way to do it. For the GeoCommons implementation we needed a way to store up function calls to the API and play them back once a map is fully loaded. I''ve put together a way to make it work and released it in on GitHub branch of the mxn code: http://github.com/mattconstantine/mxn/tree/master Here''s what I added to make it work: 1) A method on mxn.js called deferUntilLoaded. If a map isn''t fully loaded it stores the call in an onload array. 2) A new method on mxn.core.js called runDeferred. It will run any method that deferUntilLoaded queued up. 3) A hash lookup in mxn.geocommons.js containing a list of function calls that can be deferred. It looks something like this: deferrable: { applyOptions: true, resizeTo: true, addControls: true, ... } This makes it so an API implementer, like myself, can choose which methods can be deferred and which ones can''t. In the case of GeoCommons, addOverlay can be called anytime and everything else has to wait until our Flash gives the go ahead which we achieve with an onloaded callback. I used a hash here for fast lookup and to make it possible, later on, to add more granular options beyond just true/false. 4) A small tweak to invoke() in mxn.js which uses deferUntilLoaded if a method can be deferred. Take a look at the code and let me know if this might be a good addition to mxn. It only requires a few extra functions and doesn''t get in the way of anything else. It''s turned off by default and up to an individual implementer to turn it on. - Matt Constantine
Hi Matt, thanks for the addition. How would this deferring work with functions which need to return a value? The getBounds() function springs to mind as one which is called very early on and could require delaying. Kind regards, Rob 2009/6/2 Matt Constantine <designmc at gmail.com>> Hi Everyone, > > I''m a developer at ForitusOne. I''ve been working on the GeoCommons > implementation for Mapstraction v2. Andrew Turner told me that there''s > been some talk lately about whether the checkLoad functionality needs > to be there for mxn v2. I wanted to chime in about it and present some > code that demonstrates a lightweight way to do it. > > For the GeoCommons implementation we needed a way to store up function > calls to the API and play them back once a map is fully loaded. I''ve > put together a way to make it work and released it in on GitHub branch > of the mxn code: > > http://github.com/mattconstantine/mxn/tree/master > > Here''s what I added to make it work: > 1) A method on mxn.js called deferUntilLoaded. If a map isn''t fully > loaded it stores the call in an onload array. > 2) A new method on mxn.core.js called runDeferred. It will run any > method that deferUntilLoaded queued up. > 3) A hash lookup in mxn.geocommons.js containing a list of function > calls that can be deferred. It looks something like this: > > deferrable: { > applyOptions: true, > resizeTo: true, > addControls: true, > ... > } > > This makes it so an API implementer, like myself, can choose which > methods can be deferred and which ones can''t. In the case of > GeoCommons, addOverlay can be called anytime and everything else has > to wait until our Flash gives the go ahead which we achieve with an > onloaded callback. > > I used a hash here for fast lookup and to make it possible, later on, > to add more granular options beyond just true/false. > > 4) A small tweak to invoke() in mxn.js which uses deferUntilLoaded if > a method can be deferred. > > Take a look at the code and let me know if this might be a good > addition to mxn. It only requires a few extra functions and doesn''t > get in the way of anything else. It''s turned off by default and up to > an individual implementer to turn it on. > > - Matt Constantine > _______________________________________________ > Mapstraction mailing list > Mapstraction at lists.mapstraction.com > http://lists.mapstraction.com/listinfo.cgi/mapstraction-mapstraction.com >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.mapstraction.com/pipermail/mapstraction-mapstraction.com/attachments/20090602/bfc76085/attachment.htm>
Matt Constantine
2009-Jun-02 16:23 UTC
[Mapstraction] Deferring api calls until after a map has loaded
Rob, interesting question. Here''s one way that works: var map = new mxn.Mapstraction(''example_map'',''geocommons''); map.onload.geocommons.push( function() { foo = this.getBounds(); ... } ); my_map.addOverlay({map_id: 5}); That''s a bit too hacky for my tastes though. A better solution would be to have a mxn.core function which takes an anonymous function, runs it if the map is loaded or queues if it isn''t. - Matt On Tue, Jun 2, 2009 at 8:52 AM, Rob <mapstraction at thegecko.org> wrote:> Hi Matt, > > thanks for the addition. > > How would this deferring work with functions which need to return a value? > The getBounds() function springs to mind as one which is called very early > on and could require delaying. > > Kind regards, > > Rob > > 2009/6/2 Matt Constantine <designmc at gmail.com> >> >> Hi Everyone, >> >> I''m a developer at ForitusOne. I''ve been working on the GeoCommons >> implementation for Mapstraction v2. Andrew Turner told me that there''s >> been some talk lately about whether the checkLoad functionality needs >> to be there for mxn v2. I wanted to chime in about it and present some >> code that demonstrates a lightweight way to do it. >> >> For the GeoCommons implementation we needed a way to store up function >> calls to the API and play them back once a map is fully loaded. I''ve >> put together a way to make it work and released it in on GitHub branch >> of the mxn code: >> >> http://github.com/mattconstantine/mxn/tree/master >> >> Here''s what I added to make it work: >> 1) ?A method on mxn.js called deferUntilLoaded. If a map isn''t fully >> loaded it stores the call in an onload array. >> 2) ?A new method on mxn.core.js called runDeferred. It will run any >> method that deferUntilLoaded queued up. >> 3) ?A hash lookup in mxn.geocommons.js containing a list of function >> calls that can be deferred. It looks something like this: >> >> ?deferrable: { >> ? ? ? ? ?applyOptions: true, >> ? ? ? ? ?resizeTo: true, >> ? ? ? ? ?addControls: true, >> ? ? ? ? ?... >> ?} >> >> This makes it so an API implementer, like myself, can choose which >> methods can be deferred and which ones can''t. In the case of >> GeoCommons, addOverlay can be called anytime and everything else has >> to wait until our Flash gives the go ahead which we achieve with an >> onloaded callback. >> >> I used a hash here for fast lookup and to make it possible, later on, >> to add more granular options beyond just true/false. >> >> 4) ?A small tweak to invoke() in mxn.js which uses deferUntilLoaded if >> a method can be deferred. >> >> Take a look at the code and let me know if this might be a good >> addition to mxn. It only requires a few extra functions and doesn''t >> get in the way of anything else. It''s turned off by default and up to >> an individual implementer to turn it on. >> >> - Matt Constantine >> _______________________________________________ >> Mapstraction mailing list >> Mapstraction at lists.mapstraction.com >> http://lists.mapstraction.com/listinfo.cgi/mapstraction-mapstraction.com > >