Persistent Data Test App
From OpenSocial Directory
|
|
| ||||||||||||
Image:Example.jpg==Reviews==
Add a Review
Template:Persistent Data Test App Reviews
[edit] Comments
| Leave a comment |
|---|
[edit] Code
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Sample Requests" >
<Require feature="opensocial-0.5"/>
</ModulePrefs>
<Content type="html">
<script type="text/javascript">
/**
* Sample calls for the OpenSocial API
*
* These functions show some basic uses of the social API.
*
*/
var htmlout = "";
var me = null;
_IG_RegisterOnloadHandler(requestMe);
/**
* How do I get my data?
*/
function requestMe() {
// Make a new DataRequest object. This is the base object you will
// create for all data requests
var req = opensocial.newDataRequest();
// Add a request to fetch the viewer to the current DataRequest
req.add(req.newFetchPersonRequest("VIEWER"), "viewer");
req.send(handleRequestMe);
};
/**
* How do I handle responses from fetch person requests?
*/
function handleRequestMe(data) {
// Note that we are getting the parameter we specified in the request
var viewer = data.get("viewer");
// Error handling
if (viewer.hadError()) {
//Handle error using viewer.getError()
console.log(viewer.getError());
return;
}
// If there was not an error, assign the global "me" variable
// Note the getData call to pull data out of the response object
me = viewer.getData();
// Normally you would batch the request to get friends and get the viewer
// in the same data request. These are separated in this example
// to show the request and response types separately.
requestFriends();
};
/**
* How do I get my friends??
*/
function requestFriends() {
var req = opensocial.newDataRequest();
// Add a request to fetch people to the current DataRequest.
// The parameter passed to newFetchPeopleRequest can be one of
// VIEWER_FRIENDS or OWNER_FRIENDS as needed
req.add(req.newFetchPeopleRequest("VIEWER_FRIENDS"), "viewer_friends");
// Send the request, specifying the function that will receive data
req.send(handleRequestFriends);
};
/**
* How do I handle responses from fetch people requests?
*/
function handleRequestFriends(data) {
var myfriends = data.get("viewer_friends");
if (myfriends.hadError()) {
console.log(myfriends.getError());
return;
}
// Operate on each person that is returned. Note the getData() request
myfriends.getData().each(doSomethingWithPerson);
populateMyAppData();
};
/************************************************************************
* How do I operate on Person objects?
*/
function doSomethingWithPerson(person) {
htmlout += "This person's name: " + person.getDisplayName() + "<br />";
htmlout += "This person's picture: " + person.getField("picture") + "<br />";
};
/**
* How do we set user data?
*/
function populateMyAppData() {
var req = opensocial.newDataRequest();
var data1 = Math.random() * 5;
var data2 = Math.random() * 100;
var data3 = new Date().getTime();
htmlout += "Setting AppField1 to " + data1 + "<br />";
req.add(req.newUpdatePersonAppDataRequest("VIEWER", "AppField1", data1)) + "<br />";
htmlout += "Setting AppField2 to " + data2 + "<br />";
req.add(req.newUpdatePersonAppDataRequest("VIEWER", "AppField2", data2)) + "<br />";
htmlout += "Setting AppField3 to " + data3 + "<br />";
req.add(req.newUpdatePersonAppDataRequest("VIEWER", "AppField3", data3)) + "<br />";
req.send(handlePopulateMyAppData);
};
/**
* How do I handle responses from update person app data requests?
*/
function handlePopulateMyAppData(data) {
if (data.hadError()) {
//Handle the error
console.log(data.getError());
}
requestMyData();
};
/**
* How do I fetch app data?
*/
function requestMyData() {
var req = opensocial.newDataRequest();
//Request the following three app fields
var fields = [ "AppField1", "AppField2", "AppField3" ];
req.add(req.newFetchPersonAppDataRequest("VIEWER", fields), "viewer_data");
req.send(handleRequestMyData);
};
/**
* How do I handle responses from app data requests?
*/
function handleRequestMyData(data) {
var mydata = data.get("viewer_data");
if (mydata.hadError()) {
console.log(mydata.getError());
return;
}
//Do something with the returned data - note the getData call
doSomethingWithMyData(mydata.getData());
};
/**
* How do we operate on user data?
*/
function doSomethingWithMyData(data) {
//Data is indexed by user id, and represents an object where keys
//correspond with the app data fields.
var mydata = data[me.getId()];
var div = document.getElementById('content_div');
htmlout += "My AppField1 data is: " + mydata["AppField1"] + "<br />";
htmlout += "My AppField2 data is: " + mydata["AppField2"] + "<br />";
htmlout += "My AppField3 data is: " + mydata["AppField3"] + "<br />";
div.innerHTML = htmlout;
};
</script>
<div id="content_div"></div>
]]>
</Content>
</Module>
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Sample Requests" >
<Require feature="opensocial-0.5"/>
</ModulePrefs>
<Content type="html">
<script type="text/javascript">
/**
* Sample calls for the OpenSocial API
*
* These functions show some basic uses of the social API.
*
*/
var htmlout = "";
var me = null;
_IG_RegisterOnloadHandler(requestMe);
/**
* How do I get my data?
*/
function requestMe() {
// Make a new DataRequest object. This is the base object you will
// create for all data requests
var req = opensocial.newDataRequest();
// Add a request to fetch the viewer to the current DataRequest
req.add(req.newFetchPersonRequest("VIEWER"), "viewer");
req.send(handleRequestMe);
};
/**
* How do I handle responses from fetch person requests?
*/
function handleRequestMe(data) {
// Note that we are getting the parameter we specified in the request
var viewer = data.get("viewer");
// Error handling
if (viewer.hadError()) {
//Handle error using viewer.getError()
console.log(viewer.getError());
return;
}
// If there was not an error, assign the global "me" variable
// Note the getData call to pull data out of the response object
me = viewer.getData();
// Normally you would batch the request to get friends and get the viewer
// in the same data request. These are separated in this example
// to show the request and response types separately.
requestFriends();
};
/**
* How do I get my friends??
*/
function requestFriends() {
var req = opensocial.newDataRequest();
// Add a request to fetch people to the current DataRequest.
// The parameter passed to newFetchPeopleRequest can be one of
// VIEWER_FRIENDS or OWNER_FRIENDS as needed
req.add(req.newFetchPeopleRequest("VIEWER_FRIENDS"), "viewer_friends");
// Send the request, specifying the function that will receive data
req.send(handleRequestFriends);
};
/**
* How do I handle responses from fetch people requests?
*/
function handleRequestFriends(data) {
var myfriends = data.get("viewer_friends");
if (myfriends.hadError()) {
console.log(myfriends.getError());
return;
}
// Operate on each person that is returned. Note the getData() request
myfriends.getData().each(doSomethingWithPerson);
populateMyAppData();
};
/************************************************************************
* How do I operate on Person objects?
*/
function doSomethingWithPerson(person) {
htmlout += "This person's name: " + person.getDisplayName() + "<br />";
htmlout += "This person's picture: " + person.getField("picture") + "<br />";
};
/**
* How do we set user data?
*/
function populateMyAppData() {
var req = opensocial.newDataRequest();
var data1 = Math.random() * 5;
var data2 = Math.random() * 100;
var data3 = new Date().getTime();
htmlout += "Setting AppField1 to " + data1 + "<br />";
req.add(req.newUpdatePersonAppDataRequest("VIEWER", "AppField1", data1)) + "<br />";
htmlout += "Setting AppField2 to " + data2 + "<br />";
req.add(req.newUpdatePersonAppDataRequest("VIEWER", "AppField2", data2)) + "<br />";
htmlout += "Setting AppField3 to " + data3 + "<br />";
req.add(req.newUpdatePersonAppDataRequest("VIEWER", "AppField3", data3)) + "<br />";
req.send(handlePopulateMyAppData);
};
/**
* How do I handle responses from update person app data requests?
*/
function handlePopulateMyAppData(data) {
if (data.hadError()) {
//Handle the error
console.log(data.getError());
}
requestMyData();
};
/**
* How do I fetch app data?
*/
function requestMyData() {
var req = opensocial.newDataRequest();
//Request the following three app fields
var fields = [ "AppField1", "AppField2", "AppField3" ];
req.add(req.newFetchPersonAppDataRequest("VIEWER", fields), "viewer_data");
req.send(handleRequestMyData);
};
/**
* How do I handle responses from app data requests?
*/
function handleRequestMyData(data) {
var mydata = data.get("viewer_data");
if (mydata.hadError()) {
console.log(mydata.getError());
return;
}
//Do something with the returned data - note the getData call
doSomethingWithMyData(mydata.getData());
};
/**
* How do we operate on user data?
*/
function doSomethingWithMyData(data) {
//Data is indexed by user id, and represents an object where keys
//correspond with the app data fields.
var mydata = data[me.getId()];
var div = document.getElementById('content_div');
htmlout += "My AppField1 data is: " + mydata["AppField1"] + "<br />";
htmlout += "My AppField2 data is: " + mydata["AppField2"] + "<br />";
htmlout += "My AppField3 data is: " + mydata["AppField3"] + "<br />";
div.innerHTML = htmlout;
};
</script>
<div id="content_div"></div>
]]>
</Content>
</Module>
Link: http://opensocialdirectory.org/persistentdata.xml
[edit] See Also
- Google Developer Guide: http://code.google.com/apis/opensocial/docs/javascript/index.html
- List of OpenSocial Test Applications
BlogMarks
del.icio.us
digg
Furl
Newsvine
reddit
Simpy
Slashdot