<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tutorials Archive &#187; Adobe Flex</title>
	<atom:link href="http://tutarchive.com/category/web-design-development/adobe-flex/feed/" rel="self" type="application/rss+xml" />
	<link>http://tutarchive.com</link>
	<description>Save and Share your Tutorials</description>
	<lastBuildDate>Thu, 04 Feb 2010 02:51:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Flex Alert! Listen to the buttons</title>
		<link>http://tutarchive.com/2009/09/flex-alert-listen-to-the-buttons/</link>
		<comments>http://tutarchive.com/2009/09/flex-alert-listen-to-the-buttons/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 08:32:52 +0000</pubDate>
		<dc:creator>TutArchive</dc:creator>
				<category><![CDATA[Adobe Flex]]></category>
		<category><![CDATA[Flex Alert]]></category>

		<guid isPermaLink="false">http://tutarchive.com/?p=237</guid>
		<description><![CDATA[The question of how to handle/listen to button clicks from the Alert control pops up (excuse the pun) quite often. To do this you need to create an actionscript function to handle the event. Other situations may arise where you also need to pass further information to the handler function. This post will show you [...]


Related posts:<ol><li><a href='http://tutarchive.com/2009/09/object-oriented-programming-with-javascript-timer-class/' rel='bookmark' title='Permanent Link: Object Oriented Programming with JavaScript : Timer Class'>Object Oriented Programming with JavaScript : Timer Class</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>The question of how to handle/listen to button clicks from the Alert control pops up (excuse the pun) quite often. To do this you need to create an actionscript function to handle the event. Other situations may arise where you also need to pass further information to the handler function. This post will show you how <img src="http://www.flexdaddy.com/wp-includes/images/smilies/icon_wink.gif" alt=";-)" /></p>
<p><span id="more-37"> </span></p>
<p>The mx.controls.Alert class has a static method that shows the Alert control with the title, message, and requested buttons. The following code block will demonstrate how to declare a listener for your Alert control, as well as what to do when you catch the event.</p>
<p>In short, the code shows how to create an Alert control which has 2 buttons, <strong>Yes</strong> and <strong>No</strong>. Keep an eye out for for line 12 – this is where we delegate the button click listener to the <em>handleAlert</em> method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Alert;
<span style="color: #0033ff; font-weight: bold;">import</span> mx.utils.Delegate;
&nbsp;
<span style="color: #009900;">// show the Alert control</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> showAlert<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> Void
<span style="color: #000000;">&#123;</span>
    <span style="color: #009900;">// the show method follows the method signature from the Flex Actionscript API</span>
    Alert.<span style="color: #004993;">show</span><span style="color: #000000;">&#40;</span> <span style="color: #990000;">&quot;Are you sure?&quot;</span>,
                         <span style="color: #990000;">&quot;Confirm Delete&quot;</span>,
                         Alert.YES<span style="color: #000000; font-weight: bold;">|</span> Alert.NO,
                         <span style="color: #0033ff; font-weight: bold;">null</span>,
                         Delegate.create<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #0033ff; font-weight: bold;">this</span>.handleAlert<span style="color: #000000;">&#41;</span>,
                         <span style="color: #0033ff; font-weight: bold;">null</span>,
                         Alert.YES<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900;">// handle the Alert control button click</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> handleAlert<span style="color: #000000;">&#40;</span> event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> Void
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> event.detail == Alert.YES<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #009900;">// YES button was clicked</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span> event.detail == Alert.NO<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #009900;">// NO button was clicked</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-237"></span><br />
Quite similarly we can also create an inline function, which will allow us to pass extra variables to the handler method. Take a look at the following and how it declares a function which takes an <em>event</em> object and then makes a call to our own <em>handleAlert</em> method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//line 12 replacement</span>
Delegate.create<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #339966; font-weight: bold;">function</span><span style="color: #000000;">&#40;</span>event<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span><span style="color: #0033ff; font-weight: bold;">this</span>.handleAlert<span style="color: #000000;">&#40;</span>event, <span style="color: #990000;">&quot;hello&quot;</span><span style="color: #000000;">&#41;</span>;<span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #009900;">//modified alertHandler method signature</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> handleAlert<span style="color: #000000;">&#40;</span> event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, <span style="color: #004993;">test</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">:</span> Void
<span style="color: #000000;">&#123;</span>
    <span style="color: #009900;">//handle the event.detail property etc</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>You can see that the above example passes the string hello through to our handleAlert method. Don’t forget to change the method signature for the handleAlert method to compensate for the extra variable(s).</p>
<p>Now you can not only create, but successfully listen to and handle the button clicks from within an Alert control. Keep an eye out for another post soon where I’ll demonstrate how to further customise the Alert control, but also outline the setbacks with its current implementation</p>


<p>Related posts:<ol><li><a href='http://tutarchive.com/2009/09/object-oriented-programming-with-javascript-timer-class/' rel='bookmark' title='Permanent Link: Object Oriented Programming with JavaScript : Timer Class'>Object Oriented Programming with JavaScript : Timer Class</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://tutarchive.com/2009/09/flex-alert-listen-to-the-buttons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
