Communication between modules
From MapbenderWiki
In Mapbender 2.7, you can make modules interoperate with each other. You can achieve this without editing the source code, only by editing Element vars. The idea is to listen to events in module A, and then execute a method of module B.
Contents |
Identify two modules you want to interoperate
- Let's name the modules sender and receiver. Of course you can crosslink modules, but let's take it easy for now.
The sender module has to provide an event through its API:
this.events.myEvent = new Mapbender.Event();
The receiver module has to provide a method through its API
this.myFunction = function (obj) {
// bla
};
You might want to take a look at the tutorial Write a Mapbender module first.
Connecting sender and receiver
- Go the Edit application settings interface in admin2_en.
- Edit the Element var of the receiver module by clicking the link under the module name.
- Create a new JavaScript variable with the name inputs
- in the value field, enter a JSON
[
{
"method": "<name of the receiver method>",
"linkedTo": [
{
"id": "<name of the sender module>",
"event": "<name of the sender's event>",
"attr": "<attribute name of the object that is passed to the trigger function>"
}
]
}
]
- Hints
- You can of course add multiple linkages in the linkedTo array
- You can also have multiple methods connected to events, the method object is wrapped by an array as well
- The attr attribute is optional. If not present, the whole object is passed to the method
- You can also add an attribute value to the linkedTo object. Then a constant value is passed.
- Make sure to validate your JSON
Example
Check the modules mb_digitize_geometry and mb_highlight (Check the http/plugins folder). You can combine these two, so the geometries you digitize are also highlighted in the map.
Perspective
Of course the usability really suffers when JSON needs to be edited. In the long run, it would be great to have a graphical tool, where you just drew a line to combine two modules (something like Yahoo Pipes), and select boxes for the available events, methods and object attributes. It would be very helpful to control the data flow, and make creating complex applications possible.
