Search code examples
javascripteventsfirebreath

How to fire events when plugin is loaded


In my plugin I need to be able to fire event(s) once the plugin was loaded.
I don't want to use the built in mechanism (adding it in the object params) since I need to be able to control the parameters which are sent along with the event firing.

The problem is that when I try to fire the event in onPluginReady it just doesn't fire.. While debugging I noticed that the m_proxies is empty (in JSAPIImpl::FireEvent), but if I try the same code for firing the event in the onMouseDown method then it works well.

This is my createJSAPI code:

FB::JSAPIPtr plugin::createJSAPI()
{
    this->jsApi = JSApiPtr(new pluginAPI(FB::ptr_cast<plugin>(shared_from_this()), m_host));
    return this->jsApi;
}

And this is the onPluginReady code:

void plugin::onPluginReady()
{
    this->getRootJSAPI();
    this->jsApi->fireMyEvent(this->myId);
}

and the event isn't fired, though this does:

bool plugin::onMouseDown(FB::MouseDownEvent *evt, FB::PluginWindow *)
{
    this->jsApi->fireMyEvent(this->myId);
    return false;
}

Why is that?

Thanks.


Solution

  • onPluginReady is likely to be called before your onLoad callback from the param tag gets called; that means your event handlers aren't attached yet. That's the reason that FireBreath provides the onload param callback -- it gives you a place to attach event handlers and find out that things are loaded.

    Edit to clarify from comments:

    The callback will be provided with a single parameter which contains a reference to your root JSAPI object. Note that in this case it is not the object or embed tag, just the JSAPI object, so you can use any methods or properties from there.