<?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/07/send-gifts-component.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.37294-</id>
  <updated>2009-11-16T14:51:05Z</updated>
  <title>Comments for Send Gifts Component (http://www.insideria.com/2009/07/send-gifts-component.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2009://34.37294</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/07/send-gifts-component.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=37294" title="Send Gifts Component" />
    <published>2009-07-14T13:00:00Z</published>
    <updated>2009-07-08T20:27:25Z</updated>
    <title>Send Gifts Component</title>
    <summary>Sending gifts and items are one of the favorite actions among the millions of facebook users. Friends send items to friends, &quot;buy&quot; gifts, &quot;sell&quot; virtual items etc. Honestly I am not engaged in this activity a lot on facebook, but the number of people willing to send virtual gifts, buy virtual goods is shockingly high! So this article might be underestimated on the first sight, but because I am aware of the trend of sending and receiving gifts, it is worth writing a complete article that covers the trend in actionscript. The number of possible implementations for this component is really countless and in this article we will be able to show only few of them. </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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sending gifts and items are one of the favorite actions among the millions of facebook users. Friends send items to friends, "buy" gifts, "sell" virtual items etc. Honestly I am not engaged in this activity a lot on facebook, but the number of people willing to send virtual gifts, buy virtual goods is shockingly high! So this article might be underestimated on the first sight, but because I am aware of the trend of sending and receiving gifts, it is worth writing a complete article that covers the trend in actionscript. The number of possible implementations for this component is really countless and in this article we will be able to show only few of them. </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img301.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img301.html','popup','width=575,height=474,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/06/article15_img30-thumb-400x329.png" width="400" height="329" alt="article15_img30.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Here is how we are going to create this. There will be a php layer that will read the contents of the folder containing images. Those images represent gifts. In order to this to work, we need to set up AMFPHP. We will not repeat ourselves as we discussed this already in the first article. We assume that you completed the 3rd article (http://www.insideria.com/2009/04/setting-up-amfphp-for-flash-an.html) and have the AMFPHP up and running. The point we need to concentrate on is the new PHP class file that will work with our application. So, let's create it now. </div>
<div style="text-align: left;">
Open your favorite text editor. Here is EditPlus on my side:</div>
<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img2.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img2.html','popup','width=358,height=295,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/06/article15_img2-thumb-358x295.png" width="358" height="295" alt="article15_img2.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Ok, create a new PHP file:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img3.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img3.html','popup','width=136,height=220,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/06/article15_img3-thumb-136x220.png" width="136" height="220" alt="article15_img3.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">The class that we will be creating will be called GiftReader and that will also be the name of the PHP class. Here is the very basic structure:</div>

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

&lt;?php

<span class="category1">class</span> GiftReader {
 
     <span class="category1">function</span> GiftReader(){
      }
 
}

?&gt;</pre>
</code>

</div></div> 

<div style="text-align: left;">This is the basic class along with the constructor. Next this we need to do is to upload it and  check if there are any errors. So, upload it to the amfphp/services/ folder of the application folder. Run http://yourServer.com/yourAppName/amfphp/browser/index.html in the browser and check if you have the same result as below:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img41.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img41.html','popup','width=503,height=206,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/06/article15_img4-thumb-400x163.png" width="400" height="163" alt="article15_img4.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">If we have the result like on the image, then it is the sign that we are on the right path! We can safely move on to the next step. The next step would be to create the method that will open the folder that contains images etc. and pass the files as array to flash. This will make the maintenace a lot easier then having an xml the list of files. How does the method look like? Well the method has those lines of code:</div>

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


<span class="category1">function</span> readFolder($folder){
 		$d = dir("<span class="quote">../../</span>".$folder) <span class="category1">or</span> die("<span class="quote">Wrong path: $image_file_path</span>");
 		<span class="category1">while</span> (<span class="category1">false</span> !== ($entry = $d-&gt;read())) {
  			<span class="category1">if</span>($entry != '<span class="quote">.</span>' &amp;&amp; $entry != '<span class="quote">..</span>' &amp;&amp; !is_dir($dir.$entry))
  				$images[] = $entry;
  		}
 		$d-&gt;<span class="category2">close</span>();
 		<span class="category1">return</span> $images;
 	}</pre>
</code>

</div></div> 

<div style="text-align: left;">In order for this to work, we need to have one „images&#8220; folder inside our main application folder. The path to the images folder should be: http://www.yourServer.com/yourApp/images/. Inside that folder, we need to have images that will represent the items in the facebook application. For now, I just put put one dummy image to check if the function really works.</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img5.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img5.html','popup','width=233,height=111,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/06/article15_img5-thumb-233x111.png" width="233" height="111" alt="article15_img5.png" class="mt-image-none" style="" /></a></span>
<br/>
<div style="text-align: left;">
That's enough for the start. Now let's see if the function really works. Refresh the ClassBrowser and the new methods should be visible immediately:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img6.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img6.html','popup','width=229,height=228,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/06/article15_img6-thumb-229x228.png" width="229" height="228" alt="article15_img6.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">By typing the folder name inside the field</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img7.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img7.html','popup','width=259,height=175,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/06/article15_img7-thumb-259x175.png" width="259" height="175" alt="article15_img7.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">and pressing Call we get the following result:</div>

<div style="text-align: left;">Finally we got a function that reads the contents of the folder and returns it as an array! Wow, instead of creating messy xml files that need to be updated every time we add new images, this approach makes our lives much easier as we onyl need to upload new images and the php will always read all images from the folder. </div>

<div style="text-align: left;">So, by now we have a better sense of how the application will actually work. Here is the image that explains it better in detail:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img8.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img8.html','popup','width=236,height=139,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/06/article15_img8-thumb-236x139.png" width="236" height="139" alt="article15_img8.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Now, how does this apply to Flash? The Flash front end will act as the window for the images. Here are some examples of images in Gift applications:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img10.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img10.html','popup','width=740,height=426,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/06/article15_img10-thumb-400x230.png" width="400" height="230" alt="article15_img10.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">This image above is from the popular gifts application that has a lot of users and the gits are really simple images as you can see. </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img11.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img11.html','popup','width=778,height=489,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/06/article15_img11-thumb-400x251.png" width="400" height="251" alt="article15_img11.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Here is an another collection of nice gifts in the even more popular Free Gifts application. There are lots of other stuff we need to be aware of when developing such an application. In such case, we want to have rodays birthdays displayed, we want to have the news published when we send public gifts to someone, or even not if the gift is private. We also want to be able to categories the gifts and also we want to be able to search for gifts. Another great idea would be to display suggestions of newest gifts, or display gifts based on the preferences of the users. But now let's get back to reality and try to display the gifts inside the swf. </div>

<div style="text-align: left;">In order to work on this, we need to open the fla we worked on previously.Open the FLA and remove all elements from the stage.</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img12.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img12.html','popup','width=398,height=399,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/06/article15_img12-thumb-398x399.png" width="398" height="399" alt="article15_img12.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Next thing to do would be to connect to AMFPHP correctly. We already did that several times, here is the reminder: </div>

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


<span class="category1">import</span> flash.net.*;

<span class="category1">var</span> conn:<span class="category2">NetConnection</span> = <span class="category1">new</span> <span class="category2">NetConnection</span>();

conn.<span class="category2">connect</span>("<span class="quote">http://www.yourServer.com/yourApp/amfphp/gateway.php</span>");</pre>
</code>

</div></div> 

<div style="text-align: left;">Only those 2 lines of code are neccessary and we already connected to AMFPHP. We will not change anything in the lines above for the entire article and we will always use the conn property to make the method calls defined in the PHP class file.</div>
<div style="text-align: left;">
Now we will call the much discussed readFolder() method and see if the array of items actually arrives on the swf side. Here is the line of code that will make the call:</div>

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


conn.<span class="category2">call</span>("<span class="quote">GiftReader.readFolder</span>", folderResponder, "<span class="quote">images</span>");</pre>
</code>

</div></div> 

<div style="text-align: left;">And the complete frame 1 now looks like this:</div>

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


<span class="category1">import</span> flash.net.*;

<span class="category1">var</span> conn:<span class="category2">NetConnection</span> = <span class="category1">new</span> <span class="category2">NetConnection</span>();

conn.<span class="category2">connect</span>("<span class="quote">http://yourServer.com/yourApp/amfphp/gateway.php</span>");

<span class="category1">var</span> folderResponder = <span class="category1">new</span> Responder(onResult, onFault);

conn.<span class="category2">call</span>("<span class="quote">GiftReader.readFolder</span>", folderResponder, "<span class="quote">images</span>");

<span class="category1">function</span> onResult(result){
 	<span class="category2">trace</span>(result);
}

<span class="category1">function</span> onFault(result){
 	<span class="category2">trace</span>("<span class="quote">onFault: </span>" + result);
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Let's see if this actually works. We need to upload and run it inside the facebook canvas. Here is how it looks like in my window:</div>

<br/>
 <span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img13.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img13.html','popup','width=344,height=66,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/06/article15_img13-thumb-344x66.png" width="344" height="66" alt="article15_img13.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Cool, finally there is the array of images. We can now work with the images just like with normal ActionScript arrays because AMFPHP did the conversion, no need for XML files here. What's next for us?</div>

<div style="text-align: left;">Next think we need to do is to display the images in a fashion that fits our needs. For this, it's best to create a small component that will be resizable and flexible. In good software engeneering practices they say it's not good to repeat ourselves, so we will not repeat ourselves in this case. It's would be best to use components that already exist and add additional functionality to it. What could be better for this then the TileList component? Here is an image of it: </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img14.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img14.html','popup','width=394,height=271,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/06/article15_img14-thumb-394x271.jpg" width="394" height="271" alt="article15_img14.jpg" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Hey, the component seems too have all the stuff we need! It has the feature to scroll up and down, it has the feature the display images and it also givs us the features and abilities to display information about the item, like in this case. So, this leads us to the decision to use the TileList component as the front end for our gifts application! Here is what we need to do in order to display the items correctly. </div>

<div style="text-align: left;">Well, the first thing we need to do is to work with the very basic TileList. Open the Components panel (Window -> Components) </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img142.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img142.html','popup','width=174,height=426,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/06/article15_img14-thumb-174x426.png" width="174" height="426" alt="article15_img14.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Select the TileList component and drag it on the stage. The size does not matter right now.</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img15.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img15.html','popup','width=418,height=360,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/06/article15_img15-thumb-400x344.png" width="400" height="344" alt="article15_img15.png" class="mt-image-none" style="" /></a></span>
<br/>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img16.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img16.html','popup','width=344,height=165,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/06/article15_img16-thumb-344x165.png" width="344" height="165" alt="article15_img16.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">For our tutorial, we need only a few of them. The most important is, however, the dataProvider parameter. Click on the parameter to open the value window. </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img17.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img17.html','popup','width=276,height=336,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/06/article15_img17-thumb-276x336.png" width="276" height="336" alt="article15_img17.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">For the beginning, we need to add a sample image for demonstration purposes. Let's add this values:</div>

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

+
Label: Some Item
Source:  http:<span class="linecomment">//www.yourServer.com/yourApp/images/test.jpg</span></span></pre>
</code>

</div></div> 

<div style="text-align: left;">And the result is:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img18.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img18.html','popup','width=107,height=105,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/06/article15_img18-thumb-107x105.png" width="107" height="105" alt="article15_img18.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Remember that this time we need to set the absolute path to the source image, since facebook application do not recognize relative paths. This is very important to know. </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img19.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img19.html','popup','width=302,height=335,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/06/article15_img19-thumb-302x335.png" width="302" height="335" alt="article15_img19.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Ok, next thing we need to do is to tweak the component so it fits better into the application logic. We need to resize it and also, we need to set the item display to other values, the current settings are not good enough. </div>

<div style="text-align: left;">Resize the component so it fits the stage. </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img21.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img21.html','popup','width=220,height=144,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/06/article15_img21-thumb-220x144.png" width="220" height="144" alt="article15_img21.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Now when we test the application, the item looks like the following on the image:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img22.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img22.html','popup','width=141,height=147,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/06/article15_img22-thumb-141x147.png" width="141" height="147" alt="article15_img22.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">So, now we have the right look and the right size of the component, it is now almost ready and prepared to work on facebook! But wait, there is one thing to note however. From PHP, we only receive a linear array in form [img1, img2, img3, img4, img5] and the TileList dataProvider accepts an array in the form:</div>

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


0: <span class="category1">label</span>, source
1: <span class="category1">label</span>, source
2: <span class="category1">label</span>, source

etc. </pre>
</code>

</div></div> 

<div style="text-align: left;">It would be cool if we could simply pass the array and the dataProvider handles the rest. Let's extend the TileList component and add the dataProvider we want! </div>

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

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


package {
 
 	<span class="category1">import</span> fl.controls.*;
 	<span class="category1">import</span> fl.containers.*;
 
 	<span class="category1">public</span> <span class="category1">class</span> FBTileList <span class="category1">extends</span> TileList{
  
  		<span class="category1">function</span> FBTileList() {
   			<span class="category2">trace</span>("<span class="quote">Hi I am: </span>" + <span class="category1">this</span>);
   		}
  	 
  	}
 
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Save this as FBTileList.as in the project folder and change the linkage of the TileList component to FBTileList:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img23.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img23.html','popup','width=326,height=187,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/06/article15_img23-thumb-326x187.png" width="326" height="187" alt="article15_img23.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Let's check if the component really works with the new class. When compile, you should see the following result:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img24.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img24.html','popup','width=285,height=66,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/06/article15_img24-thumb-285x66.png" width="285" height="66" alt="article15_img24.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">The goal is to have a folder with items like Gift1.jpg, Gift2.jpg, Gift3.jpg. The name of the gift should be stored in the name of the file! Let's say we have 1 gift as image inside our folder. If it is an image of a TeddyBear, we should name it TeddyBear.jpg and upload it to server. The swf file will receive the list of image as array, and the swf needs to fetch those names from the image names. Here is how it can be done. Rename the 1 image file that we use as test to something more meaningful, like „SpecialGift.jpg&#8220;. </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img26.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img26.html','popup','width=189,height=114,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/06/article15_img26-thumb-189x114.png" width="189" height="114" alt="article15_img26.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Add the following function to the FBTileList Class:</div>

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


<span class="linecomment">//set images directly</span>
<span class="category1">public</span> <span class="category1">function</span> <span class="category1">set</span> items(images:<span class="category2">Array</span>):<span class="category1">void</span> {
 
 			<span class="linecomment">//create new dataprovider</span>
 			<span class="category1">var</span> dp = <span class="category1">new</span> DataProvider();
 
 			<span class="linecomment">//go through the list of images</span>
 			<span class="category1">var</span> i = 0;
 			<span class="category1">var</span> len = images.<span class="category2">length</span>;
 
 			
 			<span class="category1">for</span>(i = 0; i &lt; len; i++){
  
  				<span class="linecomment">//the image file</span>
  				<span class="category1">var</span> img = images[i];
  
  				<span class="linecomment">//split to get the name of gift</span>
  				<span class="category1">var</span> arr = img.<span class="category2">split</span>("<span class="quote">.</span>");
  
  				<span class="linecomment">//get the name</span>
  				<span class="category1">var</span> <span class="category2">name</span> = arr[0];
  
  				<span class="linecomment">//add the image and name to data provider</span>
  				dp.<span class="category2">addItem</span>({<span class="category1">label</span>: <span class="category2">name</span>, source: "<span class="quote">http://www.yourServer.com/yourFBApp/images/</span>" + img});
  			}
 
 
 			<span class="linecomment">//finally populate the own dataProvider</span>
 			<span class="category1">this</span>.dataProvider = dp;
 
 		}</pre>
</code>

</div></div> 

<div style="text-align: left;">We may not forget to change the code on the main timeline:</div>

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


<span class="category1">import</span> flash.net.*;

<span class="category1">var</span> conn:<span class="category2">NetConnection</span> = <span class="category1">new</span> <span class="category2">NetConnection</span>();

conn.<span class="category2">connect</span>("<span class="quote">http://yourServer.com/yourApp/amfphp/gateway.php</span>");

<span class="category1">var</span> folderResponder = <span class="category1">new</span> Responder(onResult, onFault);

conn.<span class="category2">call</span>("<span class="quote">GiftReader.readFolder</span>", folderResponder, "<span class="quote">images</span>");

<span class="category1">function</span> onResult(result){
 	<span class="linecomment">//set the images to item</span>
 	tileList_mc.items = result;
}

<span class="category1">function</span> onFault(result){
 	<span class="category2">trace</span>("<span class="quote">onFault: </span>" + result);
}</pre>
</code>

</div></div> 

<div style="text-align: left;">Now let's see what actually happens:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img27.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img27.html','popup','width=280,height=198,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/06/article15_img27-thumb-280x198.png" width="280" height="198" alt="article15_img27.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">The image appears right on place and also, the name of the image appears directly below! This is cool since we don't have to store the name of the item on any other place like database or xml, we have the image file and the item name stored in one file. We use the image as the actual image file and data storage  Let's try to add more images to see how it behaves. We will add one more image to display it in the swf properly. </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img28.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img28.html','popup','width=131,height=109,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/06/article15_img28-thumb-131x109.png" width="131" height="109" alt="article15_img28.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">So, let's see how this looks like in the browser. We just need to refresh it:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img29.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img29.html','popup','width=257,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/06/article15_img29-thumb-257x192.png" width="257" height="192" alt="article15_img29.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Wow, it is now soooooooo simple to add new items. What could be easier to upload images to the remote folder?</div>

<div style="text-align: left;">Next thing we need to do here is to add real gift images, add more functionalyt that we look more like a real facebook gift app! </div>

<div style="text-align: left;">Here is the sample app that we will be creating:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img30.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img30.html','popup','width=575,height=474,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/06/article15_img30-thumb-400x329.png" width="400" height="329" alt="article15_img30.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Looks nice, huh? </div>

<div style="text-align: left;">The flow of the application will be like the following: The user will simply select the list of friends and the gift will be sent out. We will place the MultiFriend component from the last sample and one send button that will send out the gift. We worked hard to create the Multifriend Component, now it's turn to see it in action. Place one MultiFriend component, and one standard button above the TileList. It should look like on image below: </div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img33.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img33.html','popup','width=574,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/06/article15_img33-thumb-400x56.png" width="400" height="56" alt="article15_img33.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">Name the button „send_btn&#8220; and the friend component „selectFriend_mc&#8220;, respectively. The next thing we need to do is to create a valid facebook session and pass it to the components, so here is how the code on frame one needs to look like:</div>

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


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

<span class="category1">var</span> conn:<span class="category2">NetConnection</span> = <span class="category1">new</span> <span class="category2">NetConnection</span>();

conn.<span class="category2">connect</span>("<span class="quote">http://yourServer.com/yourApp/amfphp/gateway.php</span>");

<span class="category1">var</span> folderResponder = <span class="category1">new</span> Responder(onResult, onFault);

conn.<span class="category2">call</span>("<span class="quote">GiftReader.readFolder</span>", folderResponder, "<span class="quote">images</span>");

<span class="category1">function</span> onResult(result){
 	tileList_mc.items = result;
}

<span class="category1">function</span> onFault(result){
 	<span class="category2">trace</span>("<span class="quote">onFault: </span>" + result);
}    

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

selectFriend_mc.<span class="category2">load</span>(fbook);</pre>
</code>

</div></div> 

<div style="text-align: left;">In the facebook window, we should get the following result:</div>

<br/>
<span class="mt-enclosure mt-enclosure-image" style="display: inline;"><a href="http://www.insideria.com/assets_c/2009/06/article15_img34.html" onclick="window.open('http://www.insideria.com/assets_c/2009/06/article15_img34.html','popup','width=554,height=213,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/06/article15_img34-thumb-400x153.png" width="400" height="153" alt="article15_img34.png" class="mt-image-none" style="" /></a></span>
<br/>

<div style="text-align: left;">When the user clicks on the send button, we simply „ask&#8220; the MultiFriend component to give us the ids selected:</div>

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


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

<span class="category1">var</span> conn:<span class="category2">NetConnection</span> = <span class="category1">new</span> <span class="category2">NetConnection</span>();

conn.<span class="category2">connect</span>("<span class="quote">http://yourServer.com/yourApp/amfphp/gateway.php</span>");

<span class="category1">var</span> folderResponder = <span class="category1">new</span> Responder(onResult, onFault);

conn.<span class="category2">call</span>("<span class="quote">GiftReader.readFolder</span>", folderResponder, "<span class="quote">images</span>");

<span class="category1">function</span> onResult(result){
 	tileList_mc.items = result;
}

<span class="category1">function</span> onFault(result){
 	<span class="category2">trace</span>("<span class="quote">onFault: </span>" + result);
}    

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

selectFriend_mc.<span class="category2">load</span>(fbook);

send_btn.addEventListener(MouseEvent.CLICK, clicked);

<span class="category1">function</span> clicked(e:MouseEvent):<span class="category1">void</span>{
 
 	<span class="linecomment">//selected ids</span>
 	<span class="category2">trace</span>(selectFriend_mc.selectedFriends);
 	
}</pre>
</code>

</div></div> 

<div style="text-align: left;">The clicked event is crucial for us. When it is fired, it gives us the ids of all friends selected. What we do with it, it's up to us. Unfortunately we do not have the time and space to discuss the detailed approach to sending and receiving of gifts, but we get the idea. With the ids, we have the opportunity to send emails or notifications to friends. In the previous articles we discussed how to send notifications in great detail. I recommend to use this article to continue with the application. We can send notifications that reference the image being sent etc. but feel free to be creative and make different approaches. </div>

<div style="text-align: left;">Almost can't believe it but we finished the 15th article! This means that there are 5 left. In the 5 articles that are left, we will go through every step of a facebook application development. We will create an application of medium size that will have all the standard features of a real facebook application. Fortunately there will be enough space in 5 articles to go trough every detail. I'm sure you will enjoy it so be ready next week as we delve deep into the development of a real world application. The application that we will going to create will be called „MovieExpert&#8220;, don't miss it, see ya!</div>

<p><br/>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.37294-comment:2167554</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.37294" type="text/html" href="http://www.insideria.com/2009/07/send-gifts-component.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2009/07/send-gifts-component.html#comment-2167554" />
    <title>Comment from brew norman on 2009-11-01</title>
    <author>
        <name>brew norman</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>i think you have to add import fl.data.DataProvider; to the as file.</p>

<p>however, i still get just one image with some label written on in even though there are many image in my images folder.</p>

<p>any suggestions?<br />
</p>]]>
    </content>
    <published>2009-11-02T00:14:49Z</published>
  </entry>

</feed
