= Sample Code Resources = * '''[http://lou.sandbox.hi5.com/friend/apps/entry/webvan.hi5.com/gadgets/gifts.xml Basic Gifts Tutorial]''' * '''[http://lou.sandbox.hi5.com/friend/apps/entry/www.webvan.hi5.com/friend/apps/developer/app/get/xml/5197 Gifts with i18n]''' * '''[http://lou.sandbox.hi5.com/friend/apps/entry/www.sandbox.hi5.com/friend/apps/developer/app/get/xml/5206 Template Demo]''' * [wiki:SignedRequests Validating Signed Requests] '''Gifts Tutorial[[BR]][[BR]]Basic '''OpenSocial XML {{{ #!xml }}} '''Important Meta-Data''' {{{ #!xml }}} '''Initialization''' {{{ #!xml gadgets.util.registerOnLoadHandler(init); function init() { loadFriends(); } }}} '''Fetching Friends''' {{{ #!xml function loadFriends() { var req = opensocial.newDataRequest(); req.add(req.newFetchPersonRequest('VIEWER'), 'viewer'); req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS'), 'viewerFriends'); req.send(onLoadFriends); } }}} '''Handle the Response''' {{{ #!xml function onLoadFriends(data) { var viewer = data.get('viewer').getData(); var viewerFriends = data.get('viewerFriends').getData(); html = new Array(); html.push(''); document.getElementById('friends').innerHTML = html.join(''); } }}} '''Adding App Data''' {{{ #!xml var givenGifts = {}; function giveGift() { var nut = document.getElementById('nut').value; var friend = document.getElementById('person').value; givenGifts[friend] = nut; var json = gadgets.json.stringify(givenGifts); var req = opensocial.newDataRequest(); req.add(req.newUpdatePersonAppDataRequest(opensocial.DataRequest .PersonId.VIEWER, 'gifts', json)); req.send(); } }}} '''Displaying App Data''' {{{ #!xml function updateGiftList(viewer, data, friends) { var json = data[viewer.getId()]['gifts']; if (!json) { givenGifts = {}; } try { givenGifts = gadgets.json.parse(json); } catch (e) { givenGifts = {}; } var options = ['a cashew nut', 'a peanut', 'a hazelnut', 'a red pistachio nut']; var html = new Array(); html.push('You have given:'); html.push(''); document.getElementById('given').innerHTML = html.join(''); } }}} '''Resizing the Window''' {{{ #!xml In Module element: After rendering content: gadgets.window.adjustHeight() }}} '''Creating Activity''' {{{ #!xml function createActivity(title) { var opts = new Array(); opts[opensocial.Activity.Field.TITLE] = title; var mediaItems = new Array(); var mediaItem = opensocial.newActivityMediaItem (opensocial.Activity.MediaItem.Type.IMAGE, viewer. getField(opensocial.Person.Field.THUMBNAIL_URL)); // Add a media item link if supported if(gadgets.util.hasFeature('hi5') && opensocial.getEnvironment().supportsField(opensocial.Environment. ObjectType.ACTIVITY_MEDIA_ITEM, hi5.ActivityMediaItemField.LINK)) { mediaItem.setField(hi5.ActivityMediaItemField.LINK, viewer.getField(opensocial.Person.Field. PROFILE_URL)); } mediaItems.push(mediaItem); opts[opensocial.Activity.Field.MEDIA_ITEMS] = mediaItems; var activity = opensocial.newActivity(opts); opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH); }}} '''Sending Notifications''' {{{ #!xml function sendNotifications(notification, toIds) { var params = new Object(); params[opensocial.Message.Field.TYPE] = opensocial. Message.Type.NOTIFICATION; var message = opensocial.newMessage(notification, params); opensocial.requestSendMessage(toIds, message); } var viewerLink = viewer.getField(opensocial.Person.Field.PROFILE_URL); var notification = '' + viewer.getDisplayName() + ' gave you a ' + options[nut]; var toIds = {}; toIds.push(friend); sendNotifications(notification, toIds); }}} '''Making Content Requests''' {{{ #!xml function getJournal() { gadgets.io.makeRequest('http://api.hi5.com/rest/feed/journal/userId', function(response) { var data = response.data; // do something with data }, {'METHOD' : 'GET', 'CONTENT_TYPE' : 'DOM'} ); } }}} '''Working with Views''' {{{ #!xml
In Your Application: function setSkin() { document.write(''); } }}} '''Using hi5.*''' Add to any data request: ([wiki:SamplePhotoDownloadJSCode More detailed code] has also been submitted by [http://www.pixverse.com PixVerse].) {{{ #!xml if(gadgets.util.hasFeature('hi5')) { req.add(hi5.newFetchStatusRequest('OWNER'),'ownerStatus'); req.add(hi5.newFetchPresenceRequest('VIEWER_FRIENDS'),'viewerFriendsPresence'); req.add(hi5.newFetchAlbumsRequest('OWNER'),'ownerAlbums'); } }}} Note that currently, for security purposes, we only expose the viewer's albums through this call, regardless of the value of the parameter set in the hi5.newFetchAlbumsRequest() call. Retrieving the data in your handler: {{{ #!xml if(gadgets.util.hasFeature('hi5')) { var ownerStatus = data.get('ownerStatus').getData(); var viewerFriendsPresence = data.get('viewerFriendsPresence').getData(); var ownerAlbums = data.get('ownerAlbums').getData(); } }}} See hi5 Open Social API for return values: [wiki:JsAPI] ---- = Sample Code Resources = * '''[http://lou.sandbox.hi5.com/friend/apps/entry/webvan.hi5.com/gadgets/gifts.xml Basic Gifts Tutorial]''' * '''[http://lou.sandbox.hi5.com/friend/apps/entry/www.webvan.hi5.com/friend/apps/developer/app/get/xml/5197 Gifts with i18n]''' * '''[http://lou.sandbox.hi5.com/friend/apps/entry/www.sandbox.hi5.com/friend/apps/developer/app/get/xml/5206 Template Demo]''' ----