What is a JSAPI Property?
A JSAPI Property definition consists of two or three parts:
- A getter method on your JSAPI class
- By convention we usually name these methods get_(property name)
- (optionally) A setter method on your JSAPI class
- By convention we usually name these methods set_(property name)
- A registration for your property in the constructor of your JSAPI class
Creating an empty JSAPI object
Let's create a really really basic JSAPI class. We're going to use FB::JSAPIAuto as our base class, and we strongly recommend that you do the same!
There! That was easy, wasn't it? If you pass an instance of this class back to the page, it will have two members: valid and ToString. These members are defined and registered by FB::JSAPIAuto, the base class.
Adding a New Property
Adding a Read-only Property
To add a new property to our MyPluginAPI JSAPI interface, first we need to add a getter method to our class. Lets add a read-only status string, shall we?
Note that if your read-only property is just getting a value that is set on your JSAPI object, a read-only Attribute may be the better/easier choice.
Adding a read-write property
Obviously, not all properties will be read-only, so we might want to create a property with a setter as well. This is easy! We just need to create two methods on our class – a getter and a setter.
Then to register it:
Strong dynamic typing
Even though we used
std::string as the type of our property, we could have just as easily used a
bool, or even
As always, the registerProperty call in the constructor is exactly the same. The most common use-case for this feature is to perform a task that takes a long time on another thread and then call the callback when finished; in that case you would want to save the callback so you have it later when you need it.
For a complete list of supported types, see Supported JSAPI types