What's new? | Help | Directory | Sign in
Google
                
Search
for
Updated May 07, 2008 by botelho.daniel
Labels: Documentation, Tutorial
SamplePersonRequest  
Sample of how to use _personRequest component

How to use PersonRequest to get the Viewer and Owner ?

In the other example we have covered the application setup so this will not be covered again in this part. So first we need to go to "apps/opensocial/modules/application/templates/indexSuccess.php" and set the Component that is going to handle this request:

<?php include_component('application','personRequest') ?>

Next thing is to configure the component action in the "/apps/opensocial/modules/application/actions/components.class.php":

public function executePersonRequest()
  {
    $this->viewer = new OSViewer('viewer');
    $this->owner = new OSOwner('owner');
    
    $this->viewer_request = new OSPersonRequest($this->viewer);
    $this->viewer_request->addProfileDetails(OSPersonField::PROFILE_URL);
    
    $this->owner_request = new OSPersonRequest($this->owner);
    $this->owner_request->addProfileDetails(OSPersonField::PROFILE_URL);
    
    $data_request = new OSDataRequest('requestPerson');
    $data_request->addRequest($this->viewer_request);
    $data_request->addRequest($this->owner_request);
    
    $this->data_response = $data_request->send(true);
    echo JSFunction::addJSTags(GadgetsUtil::registerOnLoadHandler($data_request->getDataRequestFunction()));
  }

Now lets take some time to understand this code. Here we have created two variables:

...
$this->viewer = new OSViewer('viewer');
$this->owner = new OSOwner('owner');
...

This variables are going to be used in the "view". Next we've created to OSPersonRequest objects, one for the "viewer" and other for the "owner":

...
$this->viewer_request = new OSPersonRequest($this->viewer);
...
$this->owner_request = new OSPersonRequest($this->owner);
...

After doing this we have to create an instance of the OSDataRequest and add to it those two request's that we want to make to the container:

...
$data_request = new OSDataRequest('requestPerson');
$data_request->addRequest($this->viewer_request);
$data_request->addRequest($this->owner_request);
...

Now that we have the OSDataRequest created we need to send it to the container like this:

...
$this->data_response = $data_request->send(true);
echo JSFunction::addJSTags(GadgetsUtil::registerOnLoadHandler($data_request->getDataRequestFunction()));

The last line just says to the container that the OSDataRequest created is going to be executed when the appliction is loaded.

Now we are going to see the "view" that is located at "apps/opensocial/modules/application/templates/personRequest":

<div id="viewer"></div>
<div id="owner"></div>

<script type="text/javascript">
function <? echo $data_response->getJSFunction()->getName() ?>(<? echo $data_response->getJSFunction()->getArgs() ?>) 
{
  var html = '';
  <?php echo $data_response->getData($owner_request); ?>
  var owner_name = <?php echo $owner->getName(); ?>
  var owner_thumb = <?php echo $owner->getField(OSPersonField::THUMBNAIL_URL); ?>
  var owner_profile = <?php echo $owner->getField(OSPersonField::PROFILE_URL); ?>
  var owner_nick = <?php echo $owner->getField(OSPersonField::NICKNAME); ?>
  html = '<a href="' + owner_profile + '" target="_top" style="float:left">' +'<img src="' + owner_thumb + '"/></a>' +
    owner_name + ' - '+owner_nick+'</a>'; 
  document.getElementById("owner").innerHTML = html;

  <?php echo $data_response->getData($viewer_request); ?>
  var viewer_name = <?php echo $viewer->getName(); ?>
  var viewer_thumb = <?php echo $viewer->getField(OSPersonField::THUMBNAIL_URL); ?>
  var viewer_profile = <?php echo $viewer->getField(OSPersonField::PROFILE_URL); ?>
  var viewer_nick = <?php echo $viewer->getField(OSPersonField::NICKNAME); ?>
  html = '<a href="' + viewer_profile + '" target="_top" style="float:left">' +'<img src="' + viewer_thumb + '"/></a>' +
    viewer_name + ' - '+viewer_nick+'</a>'; 
  document.getElementById("viewer").innerHTML = html
  <?php echo GadgetsWindow::adjustHeight(); ?>
  
}
</script>

Let's look at this line:

function <? echo $data_response->getJSFunction()->getName() ?>(<? echo $data_response->getJSFunction()->getArgs() ?>)

Here we use the $data_response object that was generated when we've sent the OSDataRequest to the container. The $data_response is an instance of the OSDataResponse class, and this class can be handled as an JSFunction. This means that we could setup the OSDataResponse all in the Action and didn't need to even use it in the "view", but using it in the "view" gives more flexibility.

Another important part in this example is the:

  <?php echo $data_response->getData($owner_request); ?>

For us to be able to use the "$owner" or "$viewer" object, first we need to fetch that Data from the response. After doing this we can use access directly to the objects that we've created in the Action.

The last thing from this example is this line:

<?php echo GadgetsWindow::adjustHeight(); ?> 

Here we are calling one function that is from the class gadgets.window that's in the Feature-Specific JavaScript Libraries. Calling that function generates this javascript code:

 
gadgets.window.adjustHeight(); 

and there's nothing restricting you to choose to write that code instead of calling or specific static function from "GadgetsWindow::adjustHeight()". The main difference between using our object or just writing the code your self is that, if you write that code you would need to manually import "<Require feature="dynamic-height" /> " to you're gadget( either, by setting in the "app.yml" or by calling the function OSConfig::importFeatureDynamicHeight()), and if you use "GadgetsWindow" instead you don't need to worry about that because it will be imported automatically for you.


Sign in to add a comment