Sunday, October 14, 2007

Facebook released FBJS

Marcel Laverdet of Facebook blogged about the release of FBJS 1.0:

If you are already used to Javascript, you will find that most of the syntax and functionality that you have come to know and love (or hate) is available in FBJS. Additionally, we’ve created hooks into our higher-level AJAX and dialog implementations which allow you to easily create dynamic experiences while maintaining the look and feel of Facebook.

We hope that FBJS enables you to build deeply integrated Facebook Platform applications in new and interesting ways.

FBJS does munging on the JavaScript that you provide, as it tries to stop naughty things.

With respect to Ajax, they give you an object to work with that sits on top of XHR, and is proxied by Facebook servers:

FBJS supplies a very powerful AJAX object for developers. Facebook will proxy all AJAX requests and optionally run useful post-processing on the data returned, such as JSON, or FBML parsing. To use it, just instantiate a new Ajax class. It supports the following properties:

ondone(data)
An event handler which fires when an AJAX call returns. Depending on .responseType, data will either be an object, a raw string, or an FBML string.
onerror
An event handler which fires when an error occurs during an AJAX call
requireLogin
If you set this to true the AJAX call will require the user to be logged into your application before the AJAX call will go through. The AJAX call will then be made with the regular fb_sig parameters containing the user’s identity. If they refuse to login, the AJAX call will fail.
responseType
This can be one of Ajax.RAW, Ajax.JSON, or Ajax.FBML.
Ajax.RAW
The response from your server will be returned to your callback in its original form.
Ajax.JSON
The response from your server will be parsed as a JSON object and returned to your callback in the form of an object. Properties of your JSON object which are prefixed with “fbml_” will be parsed as individual FBML strings and returned as FBML blocks. These blocks can used on a DOM object with the setInnerFBML method.
Ajax.FBML
The response from your server will be parsed as FBML and returned as an FBML block. This block can used on a DOM object with the setInnerFBML method.

And one method:


post(url, query)
Start an AJAX post. url must be a remote address, and query can be either a string or an object which will be automatically converted to a string.

Here’s an example showing most of the functionality of Ajax:
Ajax Example

It is interesting to see more and more platforms wanting to open up and give users more abilities, but keeping the balance wrt security, privacy, and general abuse.

There are also rumors of new functionality to come, such as a data storage API.

No comments:

Toplist