<?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>FlexOOP &#187; MenuItemWithChildren</title>
	<atom:link href="http://flexoop.com/tag/menuitemwithchildren/feed/" rel="self" type="application/rss+xml" />
	<link>http://flexoop.com</link>
	<description>Flex, AIR, ColdFusion, and everything in between</description>
	<lastBuildDate>Tue, 13 Jul 2010 03:56:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Update to MenuItem class</title>
		<link>http://flexoop.com/2009/02/update-to-menuitem-class/</link>
		<comments>http://flexoop.com/2009/02/update-to-menuitem-class/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 04:35:11 +0000</pubDate>
		<dc:creator>Gareth</dc:creator>
				<category><![CDATA[Component]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[MenuItem]]></category>
		<category><![CDATA[MenuItemWithChildren]]></category>

		<guid isPermaLink="false">http://flexoop.com/?p=156</guid>
		<description><![CDATA[I have recently begun using my MenuItem and MenuItemWithChildren classes at my work, and have made a slight modification to the code to lessen some of the code writing required. As I was using the menuItem class, I realized that when I selected the item, I was usually just calling some function based upon the [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently begun using my <a href="/2008/11/menuitem-class/" target"_blank">MenuItem and MenuItemWithChildren</a> classes at my work, and have made a slight modification to the code to lessen some of the code writing required.</p>
<p>As I was using the menuItem class, I realized that when I selected the item, I was usually just calling some function based upon the label of that menu item.  Using my updated MenuItem class, you can now pass in a function as one of the parameters.</p>
<p>My MenuItem class now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">flexoop</span>.<span style="color: #006600;">utility</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MenuItem <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * properties
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _enabled:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _groupName:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _icon:<span style="color: #000000; font-weight: bold;">Class</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _label:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _menuHandler:<span style="color: #000000; font-weight: bold;">Function</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _toggled:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _type:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * getters
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">enabled</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._enabled;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> groupName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._groupName;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> icon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #000000; font-weight: bold;">Class</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._icon;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> label<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._label;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> menuHandler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._menuHandler;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> toggled<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._toggled;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._type;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * setters
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">enabled</span><span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._enabled = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> groupName<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._groupName = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> icon<span style="color: #66cc66;">&#40;</span> value:<span style="color: #000000; font-weight: bold;">Class</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._icon = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> label<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._label = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> menuHandler<span style="color: #66cc66;">&#40;</span> value:<span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._menuHandler = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> toggled<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._toggled = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Inspectable<span style="color: #66cc66;">&#40;</span>enumeration=separator,check,radio,normal<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._type = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MenuItem<span style="color: #66cc66;">&#40;</span> label:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;&quot;</span>, menuHandler:<span style="color: #000000; font-weight: bold;">Function</span>=<span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #0066CC;">enabled</span>:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;normal&quot;</span>, toggled:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span>, groupName:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;&quot;</span>, icon:<span style="color: #000000; font-weight: bold;">Class</span>=<span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">enabled</span> = <span style="color: #0066CC;">enabled</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">groupName</span> = groupName;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">icon</span> = icon;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">label</span> = label;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">menuHandler</span> = menuHandler;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">toggled</span> = toggled;
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">type</span> = <span style="color: #0066CC;">type</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And similarly for the MenuItemWithChildren class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">flexoop</span>.<span style="color: #006600;">utility</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MenuItemWithChildren <span style="color: #0066CC;">extends</span> MenuItem <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * properties
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _children:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * getters
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> children<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._children;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * setters
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> children<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._children = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MenuItemWithChildren<span style="color: #66cc66;">&#40;</span> label:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;&quot;</span>, menuHandler:<span style="color: #000000; font-weight: bold;">Function</span>=<span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #0066CC;">enabled</span>:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;normal&quot;</span>, toggled:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span>, groupName:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;&quot;</span>, icon:<span style="color: #000000; font-weight: bold;">Class</span>=<span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span> label, menuHandler, <span style="color: #0066CC;">enabled</span>, <span style="color: #0066CC;">type</span>, toggled, groupName, icon <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The 2nd parameter is now the menuHandler function.  So in order to use this, you could set up something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>mx:Application xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;absolute&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;init()&quot;</span> xmlns:flexoop=<span style="color: #ff0000;">&quot;com.flexoop.utility.*&quot;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
			<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;
&nbsp;
			<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">MenuEvent</span>;
			<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">controls</span>.<span style="color: #006600;">Alert</span>;
&nbsp;
			<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">flexoop</span>.<span style="color: #006600;">utility</span>.<span style="color: #006600;">MenuItemWithChildren</span>;
			<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">flexoop</span>.<span style="color: #006600;">utility</span>.<span style="color: #006600;">MenuItem</span>;
&nbsp;
			<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> menuData:ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection;
&nbsp;
			<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> menuItem:MenuItemWithChildren = <span style="color: #000000; font-weight: bold;">new</span> MenuItemWithChildren<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test 1&quot;</span>, showMe <span style="color: #66cc66;">&#41;</span>;
				menuItem.<span style="color: #006600;">children</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MenuItem<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test 4&quot;</span>, showMe <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				menuItem.<span style="color: #006600;">children</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MenuItem<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test 5&quot;</span>, showMe <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				menuData.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> menuItem <span style="color: #66cc66;">&#41;</span>;
				menuData.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MenuItem<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test2&quot;</span>, showMe <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				menuData.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MenuItem<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test3&quot;</span>, showMe <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				mb.<span style="color: #006600;">dataProvider</span> = menuData;
&nbsp;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> menuItemHandler<span style="color: #66cc66;">&#40;</span> event:MenuEvent <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #66cc66;">&#40;</span> event.<span style="color: #006600;">item</span> as MenuItem <span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">menuHandler</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> showMe<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
				Alert.<span style="color: #0066CC;">show</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'this was fired from my menuItem'</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:VBox<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:ApplicationControlBar id=<span style="color: #ff0000;">&quot;acb&quot;</span><span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>mx:MenuBar id=<span style="color: #ff0000;">&quot;mb&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{ menuData }&quot;</span> click=<span style="color: #ff0000;">&quot;menuItemHandler( event )&quot;</span> <span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>mx:ApplicationControlBar<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:VBox<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:Application<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>(I didn&#8217;t get a chance to test the above example code so, if anyone finds that something doesn&#8217;t work correctly, feel free to ping me about it and I&#8217;ll try to get it working properly).  It&#8217;s a somewhat basic example as all items point to the same function, but it would be very simple to alter this so each menuItem points to a separate method instead, or pass a certain value to the function to display.</p>
]]></content:encoded>
			<wfw:commentRss>http://flexoop.com/2009/02/update-to-menuitem-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MenuItem Class</title>
		<link>http://flexoop.com/2008/11/menuitem-class/</link>
		<comments>http://flexoop.com/2008/11/menuitem-class/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 23:25:27 +0000</pubDate>
		<dc:creator>Gareth</dc:creator>
				<category><![CDATA[Component]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[MenuItem]]></category>
		<category><![CDATA[MenuItemWithChildren]]></category>

		<guid isPermaLink="false">http://flexfusion.archfamily.com/?p=55</guid>
		<description><![CDATA[I am not completely sure why Adobe decided not to include this in their base classes when creating the Flex framework. They have, more or less, given all of the class structure in the definition of what a menuitem must contain&#8230;enabled, toggle, groupName, etc. I decided that there&#8217;s absolutely no point in rewriting the same [...]]]></description>
			<content:encoded><![CDATA[<p>I am not completely sure why Adobe decided not to include this in their base classes when creating the Flex framework.  They have, more or less, given all of the class structure in the definition of what a menuitem <a href="http://livedocs.adobe.com/flex/201/langref/mx/controls/Menu.html">must contain</a>&#8230;enabled, toggle, groupName, etc.  I decided that there&#8217;s absolutely no point in rewriting the same thing over and over again, so why not create a MenuItem class.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">archfamily</span>.<span style="color: #006600;">utility</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MenuItem <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * properties
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _enabled:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _groupName:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _icon:<span style="color: #000000; font-weight: bold;">Class</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _label:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _toggled:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _type:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * getters
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">enabled</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._enabled;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> groupName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._groupName;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> icon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #000000; font-weight: bold;">Class</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._icon;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> label<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._label;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> toggled<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._toggled;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._type;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * setters
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">enabled</span><span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._enabled = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> groupName<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._groupName = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> icon<span style="color: #66cc66;">&#40;</span> value:<span style="color: #000000; font-weight: bold;">Class</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._icon = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> label<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._label = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> toggled<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._toggled = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Inspectable<span style="color: #66cc66;">&#40;</span>enumeration=separator,check,radio,normal<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._type = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MenuItem<span style="color: #66cc66;">&#40;</span> label:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;&quot;</span>, <span style="color: #0066CC;">enabled</span>:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;normal&quot;</span>, toggled:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span>, groupName:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;&quot;</span>, icon:<span style="color: #000000; font-weight: bold;">Class</span>=<span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">enabled</span> = <span style="color: #0066CC;">enabled</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">groupName</span> = groupName;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">icon</span> = icon;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">label</span> = label;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">toggled</span> = toggled;
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">type</span> = <span style="color: #0066CC;">type</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I added the properties to the constructor so that the menu items could be created without have to first create a variable for them, but could rather just do something like &#8220;myArray.push( new MenuItem( &#8220;myLabel&#8221;, true ) )&#8221;. Initially I added the &#8220;children&#8221; as a property of this class, but then I found out that this will cause the menu items to show an arrow next to their name as if the MenuItem actually had children (even though the item is an empty array).  In order to circumvent this problem, I decided to create a second class, MenuItemWithChildren, that extends MenuItem, but adds the property &#8220;children&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">archfamily</span>.<span style="color: #006600;">utility</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MenuItemWithChildren <span style="color: #0066CC;">extends</span> MenuItem <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * properties
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _children:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * getters
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> children<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._children;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/***********************************
		 * setters
		 **********************************/</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> children<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>._children = value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MenuItemWithChildren<span style="color: #66cc66;">&#40;</span>label:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;&quot;</span>, <span style="color: #0066CC;">enabled</span>:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span>, <span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;normal&quot;</span>, toggled:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">true</span>, groupName:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;&quot;</span>, icon:<span style="color: #000000; font-weight: bold;">Class</span>=<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>label, <span style="color: #0066CC;">enabled</span>, <span style="color: #0066CC;">type</span>, toggled, groupName, icon<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, whenever I want to create a menu item within one of my menu bars, all I have to do is create an array that holds all of my MenuItem or MenuItemWithChildren classes, and I have strongly typed class rather than a plain, old array of generic objects.  The one other thing to note is that if the menu is not created in the property declarations, then you have to set the array to the dataProvider of the menu item, or it will not refresh the view and you will not be able to see your menu items.  The implementation would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>mx:Application xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;absolute&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;init()&quot;</span> xmlns:archfamily=<span style="color: #ff0000;">&quot;com.archfamily.utility.*&quot;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:Script<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
			<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">archfamily</span>.<span style="color: #006600;">utility</span>.<span style="color: #006600;">MenuItemWithChildren</span>;
			<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">archfamily</span>.<span style="color: #006600;">utility</span>.<span style="color: #006600;">MenuItem</span>;
&nbsp;
			<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> menuData:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
			<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> menuItem:MenuItemWithChildren = <span style="color: #000000; font-weight: bold;">new</span> MenuItemWithChildren<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test 1&quot;</span>, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
				menuItem.<span style="color: #006600;">children</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MenuItem<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test 4&quot;</span>, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				menuItem.<span style="color: #006600;">children</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MenuItem<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test 5&quot;</span>, <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				menuData.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> menuItem <span style="color: #66cc66;">&#41;</span>;
				menuData.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MenuItem<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test2&quot;</span>, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				menuData.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> MenuItem<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;test3&quot;</span>, <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
				mb.<span style="color: #006600;">dataProvider</span> = menuData;
&nbsp;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:VBox<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:ApplicationControlBar id=<span style="color: #ff0000;">&quot;acb&quot;</span><span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>mx:MenuBar id=<span style="color: #ff0000;">&quot;mb&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{ menuData }&quot;</span> <span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>mx:ApplicationControlBar<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:VBox<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:Application<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>I haven&#8217;t had to use the Menu class very often yet, but I&#8217;m sure this will come in handy to someone else.  If nothing else, it saves typing in Flex Builder.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://flexoop.com/2008/11/menuitem-class/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
