Inter-widget communication

Inter-widget communication

Widgets can communicate at different levels and in different manners. They can make use of MovimRPC to directly call other widgets, or they can trigger javascript events.

For more information about MovimRPC, see the specification. This page only relates to javascripts events.

Triggering events

Events are triggered whenever you feel like it, but generally at a sensible moment, for instance when the user selects a contact. It can be done manually by calling the javascript function movim_events_emit() or using a PHP wrapper when you build your widget.

Javascript call:

<input type="button" onclick="javascript:movim_events_emit('myEvent');" />

PHP Wrapper:

function build()
{
    echo '<input type="button" onclick="'.$this->invokeJsEvent('myEvent').'" />';
}

Handling events

As for the event trigger, you can either choose to implement the handlers in javascript or in PHP. There is a big difference though, implementing handlers in PHP means that the event will need to call an ajax function, thus consuming time. This is ideal for calls that need to interact with XMPP or Movim, but not for GUI-only operations. Making handlers in javascript is not difficult. You will need to write them in a separate js file bundled with your widget.

Example javascript file:

function dothing()
{
    alert("Argh! I've been called!");
}

// Registering the handler
movim_add_event_handler("myEvent", dothing);

PHP implementation:

function WidgetLoad()
{
    $this->makeJsHandler('myEvent', 'ajaxThing');
}

function ajaxThing()
{
    $xmpp = Jabber::getInstance();
    $xmpp->sendMessage('movim@movim.eu', 'You suck man!');
}


In Other Languages
Translations of this page: