XPIDL:Function modifier
From MDC
This page has been flagged by editors or users as needing technical review
Until it is fully reviewed, it may contain inaccurate or incorrect information.
[edit] Overview
Adding the 'function' modifier to an IDL interface allows you to pass a function (such as a Javascript function) instead of an object that implements this interface anywhere that this interface is expected. An example of this is an XPCOM method that takes this interface as an argument.
All methods defined on this interface will call the passed function.
[edit] Example
(IDL File)
[function, scriptable, uuid(13e630b8-2f41-456b-ae26-c30b201c8f99)]
interface ICallback : nsISupports
{
boolean go(in PRUint32 data);
}
[scriptable, uuid(98bf0a93-577d-49dc-a9c4-23c172ebd0df)]
interface IFoo : nsISupports
{
void sum(in PRUint32 first, in PRUint32 second, in ICallback aCallback);
}
(C++ Implementation)
NS_IMETHODIMP IFoo::Sum(PRUint32 first, PRUint32 second, ICallback *aCallback)
{
PRBool ret = TRUE;
nsCOMPtr<ICallback> js_callback = aCallback;
js_callback->Go(first+second, &ret);
printf("Javascript returned: %d", ret);
}
(Javascript)
var callback = function (sum) {
alert(sum);
return true;
};
var foo = Components.classes["XXXXXX"].getService(Components.interfaces.IFoo);
foo.sum(1, 2, callback);
(Example based on http://blog.xiaoduo.info/?p=38)
NOTE: It is considered more correct to pass an object that contains the method you would like invoked, rather than using 'function', if possible.
var obj = { go: callback };
foo.sum(1, 2, obj);