<?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://insideria.com/2009/11/flex-template-components-rock.html" />
  <link rel="self" type="application/atom+xml" href="http://insideria.com/atom.xml" />
  <id>tag:insideria.com,2010://34/tag:www.insideria.com,2009://34.38481-</id>
  <updated>2010-07-19T13:36:42Z</updated>
  <title>Comments for Flex Template Components Rock (http://insideria.com/2009/11/flex-template-components-rock.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2009://34.38481</id>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/11/flex-template-components-rock.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=38481" title="Flex Template Components Rock" />
    <published>2009-11-16T20:00:00Z</published>
    <updated>2009-11-16T20:00:00Z</updated>
    <title>Flex Template Components Rock</title>
    <summary> Flex comes with a full spectrum of components, but once in a while you get hit with a request to do something that is difficult to shoehorn into what already exists, but seems even more difficult to do as...</summary>
    <author>
      <name>Amy Blankenship</name>
      
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://insideria.com/">
      <![CDATA[
<div class="ap_r"><a href="http://www.insideria.com/upload/2009/11/datagrid_flyout.PNG" class="highslide" onclick="return hs.expand(this)"><img src="http://www.insideria.com/upload/2009/11/datagrid_flyout.PNG" alt="Screen capture of datagrid flyout component" title="Click to enlarge" width="148"/></a></div>
<p>Flex comes with a full spectrum of components, but once in a while you get hit with a request to do something that is difficult to shoehorn into what already exists, but seems even more difficult to do as a custom component.  For instance, one of my clients recently asked me to have something that is essentially a master/detail view, where the master view looks like a DataGrid and the detail view also looks like a datagrid &quot;flyout&quot; to the right, vertically centered on the item that was clicked with a &quot;triangle&quot; pointing from the master to the detail.  </p>
<p>I showed him a prototype using an AdvancedDataGrid, but he had a very clear vision of his requirements, and it didn't look like an AdvancedDataGrid. This left me with a thorny problem. Yes, I could create a custom component to try to do all this, but a DataGrid is a complicated animal. I couldn't imagine trying to provide enough custom properties to set up and lay out <em>two</em> of them&#8211;not because it was beyond my capabilities, but because when I needed to consume it I didn't want to be sitting there with my documentation trying to feed all that stuff in. This is <em>especially</em> true since DataGrids are pretty simple to set up in MXML. Why would I duplicate what Adobe has already done?</p>
<h2>Enter Template Components</h2>
<p>First, what is a Template component? A Template component is a component that provides certain base functionality, but is designed to also have components added into it either in MXML or ActionScript. A perfect example of this is DataGrid. If you just give a DataGrid a dataProvider, it does a pretty good job at displaying it, but if you add DataGridColumns to it, it does an even better job.</p>
<p>At this point, I have a guilty confession to make. About 18 months ago, I made my first try at template components and failed miserably. I knew enough about the Flex Component LifeCycle to suspect that the template component example <a href="http://livedocs.adobe.com/flex/3/html/templating_3.html">provided in the doc</a>s should probably override createChildren() rather than using a handler on the initialize event (which happens between createChildren and commitProperties), but I found that when I did this that I couldn't get the children I added through MXML to show up on the stage. So, I mentally marked template components as &quot;something to figure out when I have to.&quot;</p>
<p>So, of course, this situation seemed to be a case where creating a template component would solve a lot of problems. It was, and it made several things I hadn't thought ahead to easier as well.</p>
<h2>Advantages of Template Components</h2>
<ol>
	<li>Simplifies the addition of child components, especially in MXML (this one is obvious)</li>
	<li>Child components have access to the parent scope. For example, I used my <a href="http://flexdiary.blogspot.com/search/label/DataGrid">Datagrid_withStyle</a> component for this implementation. I was able to specify not only the dataProvider and columns in MXML, but I was also easily able to attach a styleFunction to both DataGrids without doing anything special. You could also take advantage of this to add effects to the child components as well.</li>
	<li>If you're extending a container, you can add other objects into your custom component with no special effort. This can be a bit unsettling, since it might break something in your component, but it allows you to add objects you don't need to control in code into your custom container, just as you would for any container. If you look at the screen capture of my custom component, the  Date Completed/Start and Target Date/End graphics are just standard Flex components in HBoxes, which are not managed in any way by the custom logic of my component (which extends Canvas).</li>
</ol>
<h2>Things to remember when making a Template Component</h2>
<ol>
	<li>If you're extending a Container and overriding createChildren(), remember to call super.createChildren(). When you're extending UIComponent, this matters less, but Containers work their magic through having a bunch of children you're encouraged not to think about. This is why my first attempt at a template component failed.</li>
	<li>If you have any required components, be sure to provide defaults so that the component works even if the developer using your component doesn't specify those in AS or MXML.</li>
	<li>Unless you're 100% certain that your component will always be used from MXML, be sure to provide an alternate path for your &quot;subcomponents&quot; to be initialized through their setters + the invalidation cycle. This is to account for the possibility that someone adding your component in ActionScript might call addChild on your component before setting those properties, which would then have no way to be refreshed.</li>
	<li>When using a Template Component from MXML, remember that its properties will be prefixed by the same namespace as the main component, even if the <em>value</em> of the propert is a Flex Component or a component in some other namespace.</li>
</ol>
<p>What about you? Have you created a template component? If so, please share your thoughts on what you learned.</p>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.38481-comment:2189096</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.38481" type="text/html" href="http://insideria.com/2009/11/flex-template-components-rock.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/11/flex-template-components-rock.html#comment-2189096" />
    <title>Comment from nonsense on 2009-11-16</title>
    <author>
        <name>nonsense</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>^ what a way to advertise your university website. Your website is disappointing.</p>]]>
    </content>
    <published>2009-11-16T11:58:26Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.38481-comment:2189126</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.38481" type="text/html" href="http://insideria.com/2009/11/flex-template-components-rock.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/11/flex-template-components-rock.html#comment-2189126" />
    <title>Comment from FlexMan on 2009-11-16</title>
    <author>
        <name>FlexMan</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Good article - any chance of providing an example of the component you created?</p>]]>
    </content>
    <published>2009-11-16T12:32:34Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.38481-comment:2189809</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.38481" type="text/html" href="http://insideria.com/2009/11/flex-template-components-rock.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/11/flex-template-components-rock.html#comment-2189809" />
    <title>Comment from Amy on 2009-11-16</title>
    <author>
        <name>Amy</name>
        <uri>http://flexdiary.blogspot.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://flexdiary.blogspot.com">
        <![CDATA[<p>I had a feeling someone might ask that, but the developer diary is more about ideas than code.</p>

<p>So... <a href="http://flexdiary.blogspot.com/2009/11/flex-template-component.html">http://flexdiary.blogspot.com/2009/11/flex-template-component.html</a></p>

<p>Enjoy!</p>]]>
    </content>
    <published>2009-11-16T20:58:29Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2009://34.38481-comment:2237855</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2009://34.38481" type="text/html" href="http://insideria.com/2009/11/flex-template-components-rock.html"/>
    <link rel="alternate" type="text/html" href="http://insideria.com/2009/11/flex-template-components-rock.html#comment-2237855" />
    <title>Comment from Kokumo on 2009-12-22</title>
    <author>
        <name>Kokumo</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Thank's Amy, i will try to develop a template component!</p>]]>
    </content>
    <published>2009-12-22T21:38:14Z</published>
  </entry>

</feed
