OpenSocial 定义了几个基本的社交网络对象以及获得和更新这些对象的实现方法。同时,container.js 可以将这些对象映射到 Yiqi 服务器端 API 调用。有时,它还可以扩展这些对象(稍后我们将对此做详细介绍)。您的代码将几乎毫无例外地获得 Opensocial 命名空间,但请注意,分派和数据映射是由 container.js 代码实现的。例如,以下代码是“所有者”(安装挂件的人)好友的正式 OpenSocial 请求:
代码段 1:所有者好友的基本 DataRequest
function init() {
var dataRequest = opensocial.newDataRequest();
//Create a request for the owner's friends
var friendRequest = dataRequest.newFetchPeopleRequest(opensocial.DataRequest.Group.OWNER_FRIENDS);
//Add the request for processing.
dataRequest.add(friendRequest);
//Send the request, passing in a callback.
dataRequest.send(response);
}
下面显示了如何使用上述代码:
代码段 2:OWNER_FRIENDS 的简单响应句柄
function responseCallback(dataResponse) {
var friendsData = dataResponse.get(opensocial.DataRequest.Group.OWNER_FRIENDS).getData();
friendsData.each(
function(friendData) {
var friendName = friendData.getField(opensocial.Person.Field.NAME);
var friendThumbnailUrl = friendData.getField(opensocial.Person.Field.THUMBNAIL_URL);
//Do work here with the friend's name and the friend's profile image...
}
);
}
DataRequest 和 DataResponse 是 OpenSocial 的精髓。您可以将所需的数据成批加入 DataRequest 中,并从 DataResponse 获得结果。将尽可能多的数据成批加入到单个调用中将使您的挂件程序更好地运行,并为您的用户提供更好的体验。