Page tree
Skip to end of metadata
Go to start of metadata

void JSAPIImpl::FireJSEvent ( const std::string &  eventName,
const FB::VariantMap members,
const FB::VariantList arguments 
)
protectedvirtual

Fires an event into javascript asynchronously using a W3C-compliant event parameter.

This fires an event to all handlers attached to the given event in javascript. With a W3C-compliant event parameter

IE:

document.getElementByID("plugin").attachEvent("onload", function() { alert("loaded!"); });

Firefox/Safari/Chrome/Opera:

// Note that the convention used by these browsers is that "on" is implied
document.getElementByID("plugin").addEventListener("load", function() { alert("loaded!"); }, false);;/.

You can then fire the event – from any thread – from the JSAPI object like so:

FireEvent("onload", FB::variant_list_of("param1")(2)(3.0));

Also note that registerEvent must be called from the constructor to register the event.

registerEvent("onload");
Parameters
eventNameName of the event. This event must start with "on"
members
argumentsThe arguments that should be sent to each attached event handler
See Also
registerEvent

Reimplemented in FB::JSAPIAuto.

Definition at line 185 of file JSAPIImpl.cpp.

Referenced by FireJSEvent(), and FB::JSAPIAuto::FireJSEvent().

186 {
187  if (!m_valid) // When invalidated, do nothing more
188  return;
189 
190  {
191  JSAPIImplPtr self(shared_from_this());
192  boost::recursive_mutex::scoped_lock _l(m_proxyMutex);
193  ProxyList::iterator proxyIt = m_proxies.begin();
194  while (proxyIt != m_proxies.end()) {
195  JSAPIImplPtr proxy(proxyIt->lock());
196  if (!proxy) {
197  // Since you can't use a shared_ptr in a destructor, there
198  // is no way for the proxy object to let us know when it goes
199  // away; thus when we find them, we remove them for efficiency
200  proxyIt = m_proxies.erase(proxyIt);
201  continue;
202  }
203 
204  VariantList newArgs = proxyProcessList(arguments, self, proxy);
205  VariantMap newMap = proxyProcessMap(members, self, proxy);
206 
207  proxy->FireJSEvent(eventName, newMap, newArgs);
208  ++proxyIt;
209  }
210  }
211 
212  VariantList args;
213  args.push_back(CreateEvent(shared_from_this(), eventName, members, arguments));
214 
215  {
216  EventContextMap eventMap;
217  {
218  boost::recursive_mutex::scoped_lock _l(m_eventMutex);
219  eventMap = m_eventMap;
220  }
221 
222  EventContextMap::iterator it(eventMap.begin());
223  while (it != eventMap.end()) {
224  std::pair<EventMultiMap::iterator, EventMultiMap::iterator> range = it->second.equal_range(eventName);
225  for (EventMultiMap::const_iterator eventIt = range.first; eventIt != range.second; ++eventIt) {
226  eventIt->second->InvokeAsync("", args);
227  }
228  ++it;
229  }
230  }
231 
232  // Some events are registered as a jsapi object with a method of the same name as the event
233  {
234  EventIfaceContextMap evtIfaces;
235  {
236  boost::recursive_mutex::scoped_lock _l(m_eventMutex);
237  evtIfaces = m_evtIfaces;
238  }
239 
240  EventIfaceContextMap::iterator it(evtIfaces.begin());
241  while (it != evtIfaces.end()) {
242  for (EventIFaceMap::const_iterator ifaceIt = it->second.begin(); ifaceIt != it->second.end(); ++ifaceIt) {
243  ifaceIt->second->InvokeAsync(eventName, args);
244  }
245  }
246  }
247 }
std::vector< variant > VariantList
Defines an alias representing list of variants.
Definition: APITypes.h:64
std::map< std::string, variant > VariantMap
Defines an alias representing a string -> variant map.
Definition: APITypes.h:72
  • No labels