Home Our Apps Contact Articles
Thursday, December 06, 2007

How to retrieve Person App Data.

Now that we can easily store Person App Data, let's build the functionality to retrieve it.

If you missed the first part of this tutorial you can find it here.

Skip ahead if you just want to: Download the personAppData Object >>>.

To refresh your memory, here is the personAppData object we created in the first part of this tutorial:


            var personAppData = {
                reqObj:null
                ,init:function(){
                    this.reqObj = opensocial.newDataRequest( )
                }
                ,set:function( callBack , person , key , data ){
                   if( ! this.reqObj ) this.init();
                   this.reqObj.add( this.reqObj.newUpdatePersonAppDataRequest( person , key , data ) , 'updatedData' );      
                   var _self = this;
                   this.reqObj.send( function( ret ){ 
                                            _self.sendReturn( ret , callBack );
                                    });      
                }
                ,sendReturn:function( ret , callBack ){
                   if( ret.get( 'updatedData' ).hadError() ){
                        this.log( 'An error occured during the update.' );
                        return; // terminate the function
                   } // else
                   callBack();
                   return;
                }
                ,log:function( str ){
                    alert( str );
                }
            }
    

We also stored some data using this call:


        personAppData.set( function(){
                alert('Data update complete.');
            }
            ,'VIEWER'
            ,'myData'
            ,'This is the data value.'
        );
    

Retrieving Data To add the retrieval functionality, we want to create a get() function with (3) arguments:

First thing to do is to have the Object initialize itself if it isn't already.

Then we need to add a newFetchPersonAppDataRequest() and pass on the group and keys arguments. I'm labeling the returned data 'returnedData'.

            var personAppData = {
                reqObj:null
                ,init:function(){
                    this.reqObj = opensocial.newDataRequest( )
                }
                ,set:function( callBack , person , key , data ){
                   if( ! this.reqObj ) this.init();
                   this.reqObj.add( this.reqObj.newUpdatePersonAppDataRequest( person , key , data ) , 'updatedData' );      
                   var _self = this;
                   this.reqObj.send( function( ret ){ 
                                            _self.sendReturn( ret , callBack );
                                    });      
                }
                ,sendReturn:function( ret , callBack ){
                   if( ret.get( 'updatedData' ).hadError() ){
                        this.log( 'An error occured during the update.' );
                        return; // terminate the function
                   } // else
                   callBack();
                   return;
                }
                ,get:function( callBack , group , keys ){
                    if( ! this.reqObj ) this.init();
                    this.reqObj.add( this.reqObj.newFetchPersonAppDataRequest( group , keys ) , 'returnedData' );
                }
                ,log:function( str ){
                    alert( str );
                }
            }
    

To wrap up this function, I'll grab a reference to my personAppData Object (_self, below) and send() my DataRequest Object. Remember, I'm passing the callBack function around so I can fire it when all is said and done.

            var personAppData = {
                reqObj:null
                ,init:function(){
                    this.reqObj = opensocial.newDataRequest( )
                }
                ,set:function( callBack , person , key , data ){
                   if( ! this.reqObj ) this.init();
                   this.reqObj.add( this.reqObj.newUpdatePersonAppDataRequest( person , key , data ) , 'updatedData' );      
                   var _self = this;
                   this.reqObj.send( function( ret ){ 
                                            _self.sendReturn( ret , callBack );
                                    });      
                }
                ,sendReturn:function( ret , callBack ){
                   if( ret.get( 'updatedData' ).hadError() ){
                        this.log( 'An error occured during the update.' );
                        return; // terminate the function
                   } // else
                   callBack();
                   return;
                }
                ,get:function( callBack , group , keys ){
                    if( ! this.reqObj ) this.init();
                    this.reqObj.add( this.reqObj.newFetchPersonAppDataRequest( group , keys ) , 'returnedData' );
                    var _self = this;
                    this.reqObj.send( function( ret ){
                        _self.gotData( ret , callBack );
                    });
                }
                ,log:function( str ){
                    alert( str );
                }
            }
    

Now we need to write a method for handling the returned object and parsing out the data. We'll call this method gotData().

The first step is to do some error handling. If there was an error, we'll use our log() function to spit out an error message then exit.

When I'm sure the data exists, I'll assign it to a variable. Note that we'll need to make (2) calls to get the actual data object from the returned data response, since the returned object ('ret') has a function called get() that can be used to return a Response Item, and the response item also has a getter function called getData(), which is what we'll use to get the actual object we're expecting (the one containing our data).

            var personAppData = {
                reqObj:null
                ,init:function(){
                    this.reqObj = opensocial.newDataRequest( )
                }
                ,set:function( callBack , person , key , data ){
                   if( ! this.reqObj ) this.init();
                   this.reqObj.add( this.reqObj.newUpdatePersonAppDataRequest( person , key , data ) , 'updatedData' );      
                   var _self = this;
                   this.reqObj.send( function( ret ){ 
                                            _self.sendReturn( ret , callBack );
                                    });      
                }
                ,sendReturn:function( ret , callBack ){
                   if( ret.get( 'updatedData' ).hadError() ){
                        this.log( 'An error occured during the update.' );
                        return; // terminate the function
                   } // else
                   callBack();
                   return;
                }
                ,get:function( callBack , group , keys ){
                    if(!this.reqObj) this.init();
                    this.reqObj.add( this.reqObj.newFetchPersonAppDataRequest( group , keys ), 'returnedData' );
                    var _self = this;
                    this.reqObj.send( function( ret ){
                        _self.gotData( ret , callBack );
                    })
                }
                ,gotData:function( ret , callBack ){
                    if(ret.hadError() || ret.get('returnedData').hadError()){
                            this.log('An error occured.');
                            return;
                    } // data is available
                    var data = ret.get('returnedData').getData();
                }
                ,log:function( str ){
                    alert( str );
                }
            }
    

Now we'll send the returned Object back to our custom callBack function.

            var personAppData = {
                reqObj:null
                ,init:function(){
                    this.reqObj = opensocial.newDataRequest( )
                }
                ,set:function( callBack , person , key , data ){
                   if( ! this.reqObj ) this.init();
                   this.reqObj.add( this.reqObj.newUpdatePersonAppDataRequest( person , key , data ) , 'updatedData' );      
                   var _self = this;
                   this.reqObj.send( function( ret ){ 
                                            _self.sendReturn( ret , callBack );
                                    });      
                }
                ,sendReturn:function( ret , callBack ){
                   if( ret.get( 'updatedData' ).hadError() ){
                        this.log( 'An error occured during the update.' );
                        return; // terminate the function
                   } // else
                   callBack();
                   return;
                }
                ,get:function( callBack , group , keys ){
                    if( ! this.reqObj ) this.init();
                    this.reqObj.add( this.reqObj.newFetchPersonAppDataRequest( group , keys ) , 'returnedData' );
                    var _self = this;
                    this.reqObj.send( function( ret ){
                        _self.gotData( ret , callBack );
                    });
                }
                ,gotData:function( ret , callBack ){
                        if(ret.hadError() || ret.get('returnedData').hadError()){
                                this.log('An error occured.');
                                return;
                        }
                        var data = ret.get( 'returnedData' ).getData();
                        if( typeof( data ) == 'undefined' ){
                             callBack( {} );
                             return;
                        }else callBack( data );
         }
                ,log:function( str ){
                    alert( str );
                }
            }
    

The Object (the one that will be sent to the callBack function) will look like this:


        { 
            PersonId1:{
                    dataKey1:uniqueValue1
                    ,dataKey2:uniqueValue2
                    ,dataKey3:uniqueValue3
                    ,...
                  }
            ,PersonId2:{
                    dataKey1:uniqueValue1
                    ,dataKey2:uniqueValue2
                    ,dataKey3:uniqueValue3
                    ,...
                  }
        }    
    

Finishing Up: In order to itterate through our returned object we need to use the for.. in construct, as in the example below.

To finish up, let's grab the data that we stored earlier. Also, let's assume that we've stored this data for multiple people and retrieve it for the entire 'OWNER_FRIENDS' group. Here's the code to grab the data. In this example I'm just printing it to the page and exiting:



            personAppData.get( myDataHandler , 'OWNER_FRIENDS', 'myData' ); // I stored my data under the key: 'myData'
            
            function myDataHandler( returnedObject ){ 
                    // now I can access/iterate through the data 
                    var html = ''; 
                    for( var personId in returnedObject ){ 
                        html += 'Person Id: ' + personId + ' -- Data: ' + returnedObject[personId].myData + '<br /><br />'; // myData is the data key 
                    } 
                    document.body.innerHTML = html;
              }
    

This personAppData object ended up being useful. You can download the whole thing here: Download the personAppData Object >>>.

[ Page Top ](c)2009 iWiddit.com - Privacy Policy - Feedback - SiteMap

iWiddit Logo iWiddit Opensocial Developers