<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2009://34.35801-</id>
  <updated>2009-11-16T15:02:25Z</updated>
  <title>Comments for Component Architecture for Facebook Applications (http://www.insideria.com/2009/05/components-architecture-for-fa.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2009://34.35801</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=35801" title="Component Architecture for Facebook Applications" />
    <published>2009-05-12T13:00:00Z</published>
    <updated>2009-05-18T17:01:45Z</updated>
    <title>Component Architecture for Facebook Applications</title>
    <summary>In the last tutorial we discussed the new library that was released by  Adobe. The new library from Adobe fits much more naturally into the whole architecture of facebook applications that are built using the Flash platform. We need to know that the previous approach using the PHP library is not useless. In order to create stable and useful Facebook applications we need to know about many ways how to achieve the same goal. If you followed the series from the start then you are on a good way to be able to create truly cutting edge facebook apps as we have discussed have to grab results from DB and we also make use of the ActionScript library. </summary>
    <author>
      <name>Mirza Hatipovic</name>
      
    </author>
    
    <category term="Adobe Feed" />
    
    <category term="Features" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<div style="text-align: left;">In the <a href="http://www.insideria.com/2009/05/using-the-brand-new-facebook-a.html">last tutorial</a> we discussed the new library that was released by  Adobe. The new library from Adobe fits much more naturally into the whole architecture of Facebook applications that are built using the Flash platform. We need to know that the previous approach using the PHP library is not useless. In order to create stable and useful Facebook applications we need to know about many ways to achieve the same goal. 

<div class="ap_r"><a href="http://www.insideria.com/series-facebook-dev.html"><img src="http://www.insideria.com/upload/2009/04/fb_app_6.jpg" alt="series badge" style="width: 148px;" /></a><div class="apcaption">Find the rest of this series on the <a href="http://www.insideria.com/series-facebook-dev.html">series page</a>.</div></div>

When you start with Facebook application development you will encounter a lot of situations where you will need to have alternate ways to solve problems. When dealing with application development, we always have to know that decisions made at the start of the project are not written in stone, but rather written in the sand. Basically, this means that we never need to rely on the decision made at the start of the project, mentally we must be prepared for the changes that may affect our projects. In our case, suppose we work on a Facebook application that is based on Adobe's library. OK, the library is really fancy and fit very  well into our apps, but suddenly, when the project is finished, the project manager knocks on your door and says that there is a database of Facebook users that need to be imported and its in MySQL. Well, now the knowledge from previous articles comes in handy and you simply implement a PHP class file with AMFPHP that retrieves the users from the MySQL table and the result is send to Flash in a format that is understandable by ActionScript. Cool, problem solved, and your boss agrees that you should get a higher salary. If you followed the series from the start then you are on the way to being able to create truly cutting-edge Facebook apps as we have discussed have to grab results from DB and we also make use of the ActionScript library. </div>

<div style="text-align: left;">Our goal for this article is to demonstrate how to create components that rely on Adobe's ActionScript library. For the beginning, we will create a simple DisplayStatus component that can be further customized. The great thing about the components is that it can be placed anywhere on the stage and customized. The component will appear in the components panel and any number of instances can be placed on the stage. </div>

<div style="text-align: left;">Let's get started!</div>

<div style="text-align: left;">Here is the image of the final result:</div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img101.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img101.html','popup','width=159,height=81,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img10-thumb-159x81.png" width="159" height="81" alt="article6_img10.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">Open Flash and reopen the fla we worked on previously. There we have the stuff from the previous sample. Remove all components from the stage and the code from the first frame. </div>

<em><div style="text-align: left;">Go to File -> New -> ActionScript File</div></em>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img11.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img11.html','popup','width=320,height=277,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img1-thumb-320x277.png" width="320" height="277" alt="article6_img1.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">For now, just add the following lines of code:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">public</span> <span class="category1">class</span> FacebookUIComponent{
  
  		<span class="category1">function</span> FacebookUIComponent() {
   			<span class="category2">trace</span>("<span class="quote">Hi my name is... </span>" + <span class="category1">this</span>);
   		}
  	 
  	}
 
}</pre>
</code>

</div></div> 

Save this in your Facebook app project folder in the new folder called facebookUI. We will store all our Facebook-related component class files inside this package. For now, we will leave the code and go back to the Flash stage. In the fla, we will create the MovieClip that will contain the necessary assets. As you already know,  this will enable us to export the componets as SWCs and distribute it to other developers. So, create a blank movieclip called UserStatus:
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img21.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img21.html','popup','width=470,height=192,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img2-thumb-400x163.png" width="400" height="163" alt="article6_img2.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">A typical component structure for ActionScript 3.0 components consists of 2 frames. One frame for the avatar, the other frame for the assets. In ActionScript 2.0 we had to place a stop() command at the first frame to prevent the movieclip from looping, in ActionScript 3.0 this is no longer necessary. </div>

<div style="text-align: left;">So, the frame structure looks like this:</div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img31.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img31.html','popup','width=356,height=90,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img3-thumb-356x90.png" width="356" height="90" alt="article6_img3.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">On the avatar layer in the first frame, place a rectagle without fill (hairline line style!) 400 x 22. This will act as the frame size for the component when placing on the stage. On the Assets layer, we will place the Facebook API SWC from the components panel. In the <a href="http://www.insideria.com/2009/05/using-the-brand-new-facebook-a.html">previous article</a> we discussed how to place the SWC in the proper folder. </div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img42.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img42.html','popup','width=326,height=254,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img4-thumb-326x254.png" width="326" height="254" alt="article6_img4.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">Now, since the Facebook API will be packed and exported together with the component, we need to check the options for the Facebook swc that was placed on the stage. Open the library of the fla, right click the Facebook swc and choose "Linkage...". Make sure the following options are checked:</div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img51.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img51.html','popup','width=476,height=264,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img5-thumb-400x221.png" width="400" height="221" alt="article6_img5.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">So for this example we packed all the stuff we need in order for this component to run. Lock the avatar layer and leave the assets layer unlocked. </div>

<strong><div style="text-align: left;">Extending the UIComponent class</div></strong>

<div style="text-align: left;">As we already know, the UIComponent class contains all the logic we need to handle the resizing , positioning and drawing the component. In our example we will use the component architecture to make our lives and Facebook application development easier and more fun. </div>

<div style="text-align: left;">Here is the original class code we created:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">public</span> <span class="category1">class</span> FacebookUIComponent{
  
  		<span class="category1">function</span> FacebookUIComponent() {
   			<span class="category2">trace</span>("<span class="quote">Hi my name is... </span>" + <span class="category1">this</span>);
   		}
  	 
  	}
 
}</pre>
</code>

</div></div> 

Now we first need to import the UIComponent definition and the package containing the TextField class (since the simple TextField will display the status of the user):

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">import</span> fl.core.UIComponent;
<span class="category1">import</span> flash.<span class="category2">text</span>.*;</pre>
</code>

</div></div> 

and our class we extend from the this component:

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">public</span> <span class="category1">class</span> FacebookUIComponent <span class="category1">extends</span> UIComponent{</pre>
</code>

</div></div> 

After that we need to declare the variable that holds the TextArea: 

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">private</span> <span class="category1">var</span> _status_txt:<span class="category2">TextField</span> = <span class="category1">null</span>;</pre>
</code>

</div></div> 

The constructor will be left empty:

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">function</span> FacebookUIComponent() {
}</pre>
</code>

</div></div> 

<div style="text-align: left;">When extending the UIComponent there is no need to call the super() method. Now there is the important config() method that needs to be overridden. For all of you who are not familiar with the UIComponent architecture, it's important to know that this method is automatically called after the style and invalidation infrastructure for the component is initialized.  This is the place to create display objects and add them to the display list, not the constructor. We can do some neat things here! </div>

<div style="text-align: left;">Since we are creating a component that will display only the status of the Facebook user, we only need a simple text field that will be resized accordingly. Our overridden configUI() looks like this:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
 
 	<span class="linecomment">// always call super.configUI() in your implementation</span>
 	<span class="category1">super</span>.configUI();
 
 	_status_txt = <span class="category1">new</span> <span class="category2">TextField</span>();
 	addChild(_status_txt);
 
 	_status_txt.<span class="category2">text</span> = "<span class="quote">Hello....</span>";
 
}</pre>
</code>

</div></div> 

Save this code. Just to make sure you did not miss any lines and you didn't misplace it, here is the complete class:

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	<span class="category1">import</span> flash.<span class="category2">text</span>.*;
 
 	<span class="category1">public</span> <span class="category1">class</span> FacebookUIComponent <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">private</span> <span class="category1">var</span> _status_txt:<span class="category2">TextField</span> = <span class="category1">null</span>;
  
  		<span class="category1">function</span> FacebookUIComponent() {
   			<span class="linecomment">//const. stays empty</span>
   		}
  
  		<span class="linecomment">//here we place all the necessary assets on the stage</span>
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   			<span class="linecomment">// dynamically create myMenuBar</span>
   			_status_txt = <span class="category1">new</span> <span class="category2">TextField</span>();
   			addChild(_status_txt);
   
   			_status_txt.<span class="category2">text</span> = "<span class="quote">Hello....</span>";
   
   		}
  	 
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Right-click on the UserStatus component right in the library and select "Linkage", make sure the following definitions are set:</div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img6.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img6.html','popup','width=548,height=316,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img6-thumb-400x230.png" width="400" height="230" alt="article6_img6.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">Click OK and drag one instance onto the stage (the main timeline, not the timeline of the UserStatus component), like on the image below:</div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img7.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img7.html','popup','width=424,height=223,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img7-thumb-400x210.png" width="400" height="210" alt="article6_img7.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">Compile the example, and on the screen the following should appear:</div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img8.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img8.html','popup','width=227,height=180,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img8-thumb-227x180.png" width="227" height="180" alt="article6_img8.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">Nothing fancy, but if there were no errors, we did a great job until now.  </div>

<div style="text-align: left;">So how are we going to connect this to Facebook? </div>

<div style="text-align: left;">First of all, the sample will run again on the standard Facebook canvas. In the previous example we worked on the desktop AIR sample just to demonstrate the desktop abilities of the Facebook + ActionScript combination. Just as we did in the first articles, the swf need to be uploaded again to the web server. </div>

<div style="text-align: left;">So, when uploaded here is the same swf inside the Facebook canvas:</div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img9.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img9.html','popup','width=224,height=167,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img9-thumb-224x167.png" width="224" height="167" alt="article6_img9.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">Cool, now we can start modifying the class file to work with the Facebook API. For this, we will use the library by Adobe. Since we worked in the previous sample with the library, we know the basics. </div>

<div style="text-align: left;">Open the class file FacebookUIComponent that we previously worked on. After the existing import statements, add the following statements:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoData;
<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;
<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.FacebookUser;
<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoFieldValues;
<span class="category1">import</span> com.facebook.<span class="category2">data</span>.friends.*;
<span class="category1">import</span> com.facebook.commands.users.*;
<span class="category1">import</span> com.facebook.commands.friends.*;
<span class="category1">import</span> com.facebook.net.FacebookCall;
<span class="category1">import</span> com.facebook.events.FacebookEvent;
<span class="category1">import</span> com.facebook.Facebook;</pre>
</code>

</div></div> 

<div style="text-align: left;">Since it is a Facebook application that runs in the application canvas, we do not need to call the login() methods we previously had. We can directly start calling Facebook methods. First we must declare the variables that are needed:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">private</span> <span class="category1">var</span> fbook:Facebook;
<span class="category1">private</span> <span class="category1">var</span> session:FacebookSessionUtil;
<span class="category1">private</span> <span class="category1">var</span> user:FacebookUser;</pre>
</code>

</div></div> 

<div style="text-align: left;">Then, we cannot use the configUI() method to instantiate the Facebook classes since we want the configUI() to handle only the user interface objects. We use that method, it's not forbidden, but for the sake of better code maintenance, let's create a separate method for Facebook class instantiation:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">private</span> <span class="category1">function</span> initFB():<span class="category1">void</span> {
 
 			<span class="linecomment">// instaniate</span>
 			session = <span class="category1">new</span> FacebookSessionUtil("<span class="quote">	YOUR_API_KEY</span>", "<span class="quote">YOUR_SECRET</span>", stage.loaderInfo);
 			fbook = session.facebook;
 
 			<span class="linecomment">//make the call to get the status</span>
 			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([fbook.uid],[GetInfoFieldValues.ALL_VALUES]);
 			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
 			fbook.post(<span class="category2">call</span>);
 
}</pre>
</code>

</div></div>

<div style="text-align: left;">OK, but somewhere the initFB() function needs to be called and this will be done in the constructor: </div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">function</span> FacebookUIComponent() {
 	initFB();
}</pre>
</code>

</div></div>

<div style="text-align: left;">And finally, there is the onInfo function that receives the Facebook results:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">private</span> <span class="category1">function</span> onInfo(e:FacebookEvent):<span class="category1">void</span>{
 			
 	<span class="category1">var</span> user = (e.<span class="category2">data</span> as GetInfoData).userCollection.<span class="category2">getItemAt</span>(0) as FacebookUser;
 	_status_txt.<span class="category2">text</span> = user.<span class="category2">status</span>.<span class="category2">message</span>;
 	
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Since we got the FacebookUser object, we simply read what we need, this is the status message. :-) Let's compile and upload, here is the result: </div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img102.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img102.html','popup','width=159,height=81,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img10-thumb-159x81.png" width="159" height="81" alt="article6_img10.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">The result may be a bit modest, but we just created a functional Facebook Flash component! </div>

<div style="text-align: left;">Here is the complete class code:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	<span class="category1">import</span> flash.<span class="category2">text</span>.*;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoData;
 	<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.FacebookUser;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoFieldValues;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.friends.*;
 	<span class="category1">import</span> com.facebook.commands.users.*;
 	<span class="category1">import</span> com.facebook.commands.friends.*;
 	<span class="category1">import</span> com.facebook.net.FacebookCall;
 	<span class="category1">import</span> com.facebook.events.FacebookEvent;
 	<span class="category1">import</span> com.facebook.Facebook;
 
 	<span class="category1">public</span> <span class="category1">class</span> FacebookUIComponent <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">private</span> <span class="category1">var</span> _status_txt:<span class="category2">TextField</span> = <span class="category1">null</span>;
  
  		<span class="category1">private</span> <span class="category1">var</span> fbook:Facebook;
  		<span class="category1">private</span> <span class="category1">var</span> session:FacebookSessionUtil;
  		<span class="category1">private</span> <span class="category1">var</span> user:FacebookUser;
  
  		<span class="category1">function</span> FacebookUIComponent() {
   			initFB();
   		}
  
  		<span class="category1">private</span> <span class="category1">function</span> initFB():<span class="category1">void</span> {
   
   			session = <span class="category1">new</span> FacebookSessionUtil("<span class="quote">	YOUR_API_KEY</span>", "<span class="quote">YOUR_SECRET</span>", stage.loaderInfo);			
   			fbook = session.facebook;
   
   			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([fbook.uid],[GetInfoFieldValues.ALL_VALUES]);
   			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
   			fbook.post(<span class="category2">call</span>);
   
   		}
  
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   			_status_txt = <span class="category1">new</span> <span class="category2">TextField</span>();
   			addChild(_status_txt);
   
   			_status_txt.<span class="category2">text</span> = "<span class="quote">Hello....</span>";
   
   		}
  
  		<span class="category1">private</span> <span class="category1">function</span> onInfo(e:FacebookEvent):<span class="category1">void</span>{
   			
   			<span class="category1">var</span> user = (e.<span class="category2">data</span> as GetInfoData).userCollection.<span class="category2">getItemAt</span>(0) as FacebookUser;
   			_status_txt.<span class="category2">text</span> = user.<span class="category2">status</span>.<span class="category2">message</span>;
   		
   		}
  		
  	 
  	}
 
}</pre>
</code>

</div></div> 

<strong><div style="text-align: left;">Taking care of little things</div></strong>

<div style="text-align: left;">Since we made the call we have to make sure of little things. Right now the componentis a bit raw. We will have a lot of components on our stage and imagine that for every component we have to define the API key and the secret. That would be ambiguous and would not make any sense. The best solution is in fact very simple. </div>

<div style="text-align: left;">Here is the overview:</div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/04/article6_img111.html" onclick="window.open('http://www.insideria.com/assets_c/2009/04/article6_img111.html','popup','width=462,height=231,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.insideria.com/assets_c/2009/04/article6_img11-thumb-400x200.png" width="400" height="200" alt="article6_img11.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">What does this mean? We really do not want every component that we place on the stage to make the session variable. This would be a total waste of memory and it would also hurt the DRY principle, this means that we do not want to repeat ourselves in the code. Once one session is enough for us and it will be passed to every component. Then, the component itself will do with the session object what it needs to do...in this case make a call to load the user status, the other component can use the session to display the albums and send notifications etc. Everything is much easier to maintain when it is not duplicated, like our session object.</div>

<div style="text-align: left;">So, in order for this to work we need to modify the class we FacebookUIComponent:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	<span class="category1">import</span> flash.<span class="category2">text</span>.*;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoData;
 	<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.FacebookUser;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoFieldValues;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.friends.*;
 	<span class="category1">import</span> com.facebook.commands.users.*;
 	<span class="category1">import</span> com.facebook.commands.friends.*;
 	<span class="category1">import</span> com.facebook.net.FacebookCall;
 	<span class="category1">import</span> com.facebook.events.FacebookEvent;
 	<span class="category1">import</span> com.facebook.Facebook;
 
 	<span class="category1">public</span> <span class="category1">class</span> FacebookUIComponent <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">private</span> <span class="category1">var</span> _status_txt:<span class="category2">TextField</span> = <span class="category1">null</span>;
  
  		<span class="category1">function</span> FacebookUIComponent() {
   		}
  
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   			_status_txt = <span class="category1">new</span> <span class="category2">TextField</span>();
   			addChild(_status_txt);
   
   		}
  
  		<span class="category1">public</span> <span class="category1">function</span> loadStatus(fbook:Facebook) {
   			
   			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([fbook.uid], [GetInfoFieldValues.ALL_VALUES]);
   			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
   			fbook.post(<span class="category2">call</span>);
   
   		}
  
  		<span class="category1">private</span> <span class="category1">function</span> onInfo(e:FacebookEvent):<span class="category1">void</span>{
   			
   			<span class="category1">var</span> user = (e.<span class="category2">data</span> as GetInfoData).userCollection.<span class="category2">getItemAt</span>(0) as FacebookUser;
   			_status_txt.<span class="category2">text</span> = user.<span class="category2">status</span>.<span class="category2">message</span>;
   		
   		}
  		
  	 
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">The next thing we need to do is go back to our main timeline and place those lines of code in the first frame:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">import</span> com.facebook.Facebook;
<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;

<span class="category1">var</span> fbook:Facebook;
<span class="category1">var</span> session:FacebookSessionUtil = <span class="category1">new</span> FacebookSessionUtil("<span class="quote">MY_API_KEY</span>", "<span class="quote">MY_SECRET</span>", loaderInfo);
fbook = session.facebook;

status1_mc.loadStatus(fbook);</pre>
</code>

</div></div> 

<div style="text-align: left;">The only thing that is left to do is to select the UserStatus movieclip on the stage and give it the instance name status1_mc. You can see that we reference it in the code. When compiled, the result is pretty much the same:</div>

 <div style="text-align: left;">This time the architecture is much more flexible. First we cleared the contructor:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">function</span> FacebookUIComponent() {
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Then we completely removed the initFB() function since we will not allow the component to handle the session stuff. The configUI() function is the same, only the loadStatus function is new:</div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

<span class="category1">public</span> <span class="category1">function</span> loadStatus(fbook:Facebook) {
 			
 			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([fbook.uid], [GetInfoFieldValues.ALL_VALUES]);
 			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
 			fbook.post(<span class="category2">call</span>);
 
 		}</pre>
</code>

</div></div> 

<div style="text-align: left;">So, as we can see, the method receives the Facebook object that containts all the session data. The we make our call to the Facebook API and retrieve the status. Throughout the application there will be only one session object, no need to create it for every component. </div>

<div style="text-align: left;">At the end, we really do not want the class to be called FacebookUIComponent. I named it this way to better understand the process of UIComponent inheritance. Rename the whole ActionScript class file to UserStatus.as, and the class name should now match the name. </div>

<div class="acode" style="overflow:auto;padding:10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

package facebookUI{
 
 	<span class="category1">import</span> fl.core.UIComponent;
 	<span class="category1">import</span> flash.<span class="category2">text</span>.*;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoData;
 	<span class="category1">import</span> com.facebook.utils.FacebookSessionUtil;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.FacebookUser;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.users.GetInfoFieldValues;
 	<span class="category1">import</span> com.facebook.<span class="category2">data</span>.friends.*;
 	<span class="category1">import</span> com.facebook.commands.users.*;
 	<span class="category1">import</span> com.facebook.commands.friends.*;
 	<span class="category1">import</span> com.facebook.net.FacebookCall;
 	<span class="category1">import</span> com.facebook.events.FacebookEvent;
 	<span class="category1">import</span> com.facebook.Facebook;
 
 	<span class="category1">public</span> <span class="category1">class</span> UserStatus <span class="category1">extends</span> UIComponent{
  
  		<span class="category1">private</span> <span class="category1">var</span> _status_txt:<span class="category2">TextField</span> = <span class="category1">null</span>;
  
  		<span class="category1">function</span> UserStatus() {
   		}
  
  		override protected <span class="category1">function</span> configUI():<span class="category1">void</span> {
   
   			<span class="linecomment">// always call super.configUI() in your implementation</span>
   			<span class="category1">super</span>.configUI();
   
   			_status_txt = <span class="category1">new</span> <span class="category2">TextField</span>();
   			addChild(_status_txt);
   
   		}
  
  		<span class="category1">public</span> <span class="category1">function</span> loadStatus(fbook:Facebook) {
   			
   			<span class="category1">var</span> <span class="category2">call</span>:GetInfo = <span class="category1">new</span> GetInfo([fbook.uid], [GetInfoFieldValues.ALL_VALUES]);
   			<span class="category2">call</span>.addEventListener(FacebookEvent.COMPLETE, onInfo);
   			fbook.post(<span class="category2">call</span>);
   
   		}
  
  		<span class="category1">private</span> <span class="category1">function</span> onInfo(e:FacebookEvent):<span class="category1">void</span>{
   			
   			<span class="category1">var</span> user = (e.<span class="category2">data</span> as GetInfoData).userCollection.<span class="category2">getItemAt</span>(0) as FacebookUser;
   			_status_txt.<span class="category2">text</span> = user.<span class="category2">status</span>.<span class="category2">message</span>;
   		
   		}
  		
  	 
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Believe it or not, we did a great job until now. We defined a Facebook component architecture that is flexible and that will prove useful when our Facebook applications get more complex. In such situations, the DRY priciple is always useful. We use only one Facebook session accross the whole application and pass it to specific components that accomplish specific actions. In the next article we will discuss the profile pic component that is based on the architecture that was described in this article. You may ask yourself why we made so many code changes in this article and why we didn't simply go through the last sample where everything works and is correct? It's because I wanted you try the hard way and create code that works (FacebookUIComponent.as) but is not perfect and lacks flexibility. I really wanted you to make the changes and see what benefits the application design will have.</div>

<div style="text-align: left;">I hope next week you will tune in where we discuss the component for profile pictures.</div>

<p><br/></p>

<p>To view the entire series please visit <a href="http://www.insideria.com/series-facebook-dev.html">http://www.insideria.com/series-facebook-dev.html</a></p>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35801-comment:2060083</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35801" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html#comment-2060083" />
    <title>Comment from Dara on 2009-05-21</title>
    <author>
        <name>Dara</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>If anyone gets the following error: 1017: The Definition Of Base Class UIComponent Was Not Found, see the following <br />
<a href="http://flashax.net/1017-the-definition-of-base-class-uicomponent-was-not-found/25/">Solution</a>. </p>

<p>You will need to add<br />
$(AppConfig)/Component Source/ActionScript 3.0/User Interface</p>

<p>to your AS3 application settings.</p>

<p><br />
</p>]]>
    </content>
    <published>2009-05-21T11:43:36Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35801-comment:2067533</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35801" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html#comment-2067533" />
    <title>Comment from Igor on 2009-06-30</title>
    <author>
        <name>Igor</name>
        <uri>http://www.facebook.com\igor.sekuloski</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.facebook.com\igor.sekuloski">
        <![CDATA[<p>Can you tell me how can I implement the facebook component to my ActionScript 2.0 project</p>]]>
    </content>
    <published>2009-07-01T00:23:32Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35801-comment:2067534</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35801" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html#comment-2067534" />
    <title>Comment from Igor on 2009-06-30</title>
    <author>
        <name>Igor</name>
        <uri>http://www.facebook.com\igor.sekuloski</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.facebook.com\igor.sekuloski">
        <![CDATA[<p>Can you tell me how can I implement the facebook component to my ActionScript 2.0 project</p>]]>
    </content>
    <published>2009-07-01T00:24:12Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35801-comment:2067535</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35801" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html#comment-2067535" />
    <title>Comment from Igor on 2009-06-30</title>
    <author>
        <name>Igor</name>
        <uri>http://www.facebook.com\igor.sekuloski</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.facebook.com\igor.sekuloski">
        <![CDATA[<p>Can you tell me how can I implement the facebook component to my ActionScript 2.0 project</p>]]>
    </content>
    <published>2009-07-01T00:25:48Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35801-comment:2068766</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35801" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html#comment-2068766" />
    <title>Comment from Frazko on 2009-07-21</title>
    <author>
        <name>Frazko</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>what is the "standard Facebook canvas" for you??   do you mean FBML ??</p>]]>
    </content>
    <published>2009-07-22T01:38:32Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35801-comment:2070727</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35801" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html#comment-2070727" />
    <title>Comment from Mauricio Cano on 2009-08-20</title>
    <author>
        <name>Mauricio Cano</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Hello everyone, I enjoy so much these tutorials.</p>

<p>I want to ask if I can put the information of the status in a Dynamic Text. If this is possible, what can I do?</p>

<p>Thanks if you can help me. </p>]]>
    </content>
    <published>2009-08-20T15:43:40Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35801-comment:2070729</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35801" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html#comment-2070729" />
    <title>Comment from Mauricio Cano on 2009-08-20</title>
    <author>
        <name>Mauricio Cano</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Hello everyone, I enjoy so much these tutorials.</p>

<p>I want to ask if I can put the information of the status in a Dynamic Text. If this is possible, what can I do?</p>

<p>Thanks if you can help me. </p>]]>
    </content>
    <published>2009-08-20T15:47:09Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.35801-comment:2079862</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.35801" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/05/components-architecture-for-fa.html#comment-2079862" />
    <title>Comment from Tim on 2009-09-02</title>
    <author>
        <name>Tim</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Thanks again for the detailed explanation, as per usual I can't fault it. I love the Facebook API, and particularly like doing this with it with your help! <a href="http://backpacking-in-europe.com/lightweight-rucksack/"> lightweight rucksack</a><br />
</p>]]>
    </content>
    <published>2009-09-02T20:21:07Z</published>
  </entry>

</feed
