Please feel free to add to these pages! The documentation is a wiki, and anyone can sign up. Note that all modifications will be moderated by the development team.
Every plugin class has one or more JSAPI object which provides the interface to Javascript. The primary JSAPI object is commonly referred to as the Root JSAPI object. We call something a JSAPI object if it derives from the FB::JSAPI class. Most JSAPI objects derive from the FB::JSAPIAuto helper class.
FB::JSAPI - JavaScript API base classFB::JSAPIAuto - JavaScript API Automatic helper classThe Root JSAPI object is returned by your plugin class and is the primary interface that is accessible to Javascript. Your plugin can provide additional JSAPI objects that can be returned by Methods or Properties on your Root JSAPI object.
JSAPI objects are named by convention with the suffix "API". The fbgen tool creates the root JSAPI object named the same as your plugin object with the API suffix appended. e.g. for the plugin object "MyPlugin", the JSAPI object is commonly named "MyPluginAPI".
If your object tag looks like this:
<object id="plugin" type="application/x-fbtestplugin" width="300" height="300">
<param name="onload" value="pluginLoaded" />
</object>
|
Your plugin resides then in the object tag with id="plugin". Once the plugin is loaded, you can access it using javascript through that element like so:
function doSomething() {
var plugin = document.getElementById("plugin");
alert(plugin.valid);
}
|
JSAPI exposes four basic types of interfaces to JavaScript. Each of these types must be registered in the constructor of your JSAPI object (except Attributes, which are new in 1.4, which we will talk about below.) For more information and instructions on using each of these interface types, click on the name.
plugin.doSomethingCool(3, 4, "Some string", window.myFunction); |
// This will call getValid in the JSAPI class. This property is defined by default on all JSAPIAuto objects var val = plugin.valid // This will call setSomeValue in the JSAPI class, assuming that we registered the property to have that method be the setter for SomeValue plugin.SomeValue = "my awesome string value"; |
plugin.someWeirdAttributeThatHasNeverBeenSeenBefore = 34423.5; alert(plugin.someWeirdAttributeThatHasNeverBeenSeenBefore); // Unless explicitly disallowed, this will work and will alert 34423.5 var val = plugin.SOME_CONST_VAL; // If you register a const attribute in your plugin you can use it easily as a constant from javascript |
attachEvent in IE and addEventListener in all other browsers.
plugin.attachEvent("onstuff", function(val) { alert("Stuff happened: " + val); });
// -or-
plugin.addEventListener("stuff", function(val) { alert("Stuff happened: " + val); }, false);
|
Once you have a basic understanding of how these interfaces work, you will also want to read up on the types that are supported by JSAPIAuto.