Here's a simple implementation to extend on my previous effort to make a social web site; let members / users decide whether they like a particular post.
We'll make use of GFC js lib/API.
First, load the library in our header:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('friendconnect', '0.8');
</script>
Then, put our action, in this example we're putting :
<span onClick="iLike()" id="ilike" style="background: #ddccee;">Like this post!</span><span id="xlogin"></span> #a trivial example
so that a user can click and the activity will be available to all and the user's friends.
Next , we'll put these javascript snippets anywhere before the end of body:
<script type="text/javascript">
var SITE_ID = 'replace_with_gfc_id';
var viewer, ownerFriends, activities;
google.friendconnect.container.setParentUrl('/api/' /* location of rpc_relay.html and canvas.html */);
google.friendconnect.container.loadOpenSocialApi({
site: SITE_ID,
onload: function() { initAllData(); }});
function initAllData() {
var params = {};
params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] =
[opensocial.Person.Field.ID,opensocial.Person.Field.NAME,opensocial.Person.Field.THUMBNAIL_URL,opensocial.Person.Field.PROFILE_URL];
var req = opensocial.newDataRequest();
req.add(req.newFetchPersonRequest('VIEWER', params), 'viewer');
req.add(req.newFetchPeopleRequest(
new opensocial.IdSpec({'userId' : 'OWNER', 'groupId' : 'FRIENDS'}), params),
'ownerFriends');
req.add(req.newFetchActivitiesRequest(new opensocial.IdSpec({'userId' : 'OWNER', 'groupId' : 'FRIENDS'})), 'activities');
req.send(setupData);
};
function setupData(data) {
ownerFriends = data.get('ownerFriends').getData().asArray();
viewer = data.get('viewer').getData();
activities = data.get('activities').getData();
};
function iLike() {
if (viewer) {
var activity = opensocial.newActivity({
title: viewer.getDisplayName()
+ ' likes <a href="' + document.URL + '">' + document.title + '</a>' });
opensocial.requestCreateActivity(activity, "HIGH",
function() { setTimeout(initAllData,1000); });
jQuery("#ilike").hide();
}
else{document.getElementById("xlogin").innerHTML = 'login or join ...' }
};
</script>
The function iLike will create our activity at the server (google's (?)). In this implementation, we don't keep track on the number of times users liked a post.
I need to find time to learn more; time that I don't have :P
See the implementation on this site, join/login to like a post.
What else can we do to socialize a site?
Trackback is http://myzope.kedai.com.my/blogs/kedai/232/tbping
