Using the Mapbender event system
From MapbenderWiki
This is a tutorial for Mapbender 2.7 and newer (note: this equals trunk at the moment, January 2010).
Contents |
Preface
Mapbender has some built-in events to synchronize the core with its plugins. Synchronizing is very important, if not done properly, your application will not load. You can access the events by typing
Mapbender.events.name
Read more about the Event object in the API documentation.
Events
initMap
This event triggers the map initialisation. It is triggered when
- The DOM is ready (jQuery ready function)
- The jQuery plugins have been applied (all application elements have now added their API to Mapbender)
We decided to put the map initialisation first, because most modules depend on a map.
init
This is the most important Mapbender event. It is triggered when
- The DOM is ready (jQuery ready function)
- The jQuery plugins have been applied (all application elements have now added their API to Mapbender)
- The maps have been initialised (initMap event)
Note: If you want to access the API of another module,
always register your code with the init event!
If your code is in an iframe, synchronization is not as easy. If your iframe code has loaded, you cannot be sure if the Mapbender init has already been fired. For these case we have added the boolean property Mapbender.events.init.done. You can check if the event has been triggered, if not, register your code, if yes, execute your code rightaway.
beforeInit/afterInit
These events are deprecated. They have been used to tweak the synchronisation for complex dependencies. You should not have to use these events.
beforeInit is triggered, when
- The DOM is ready (jQuery ready function)
- The jQuery plugins have been applied (all application elements have now added their API to Mapbender)
- The maps have been initialised (initMap event)
afterInit is triggered, when
- The DOM is ready (jQuery ready function)
- The jQuery plugins have been applied (all application elements have now added their API to Mapbender)
- The maps have been initialised (initMap event)
- The modules have been initialised (init event)
Another way to tweak synchronization is to toy around with the pos setting in the edit application elements dialogue. This setting is an integer, and determines the order in which the elements are loaded. However, this is for workarounds only, and not recommended.
Create your own events
Your application elements can provide their own events through the Mapbender API. Make sure you have read the tutorial on supplying API functionality in your modules.
In your constructor, just add this
this.events = {
event1: new Mapbender.Event(),
event2: new Mapbender.Event(),
event3: new Mapbender.Event(),
...
};
Other modules can access this event like this
$("#id").mapbender(function () {
this.events.event1.register(function);
});
or simply
Mapbender.modules.id.events.register(function);
