A Better Prompting TextInput with Flex 4

There are two ways to share data between a Flex 4 Spark component and its skin: you can either pull the data from the component into the skin or you can push the data to the skin from the component. It’s a subtle difference, but the latter approach is recommended because it does not rely on data binding, and it promotes encapsulation.

A while back I shared an example of using Flex 4 to create a text input that supports displaying a prompt when it is not focused and no text has been entered. In that example the skin pulled the value of the promptText property from the hostComponent and applied it to a SimpleText component with the id “promptView”. The logic for whether or not the promptView was visible was based on the current state of the skin (focused or normal) and whether or not the hostComponent's text property was an empty string. This is fine, sort of, but since a good component should be easy to skin, it would be better if the person creating the skin didn’t have to set the label on the promptView or replicate the logic to hide/show it. Because components are aware of the data type of each skin part, it is possible to push data into the skin from the component and encapsulate more of the logic.

I was getting a little tripped up there trying to explain that, but hopefully looking at the two examples side by side will make it more clear.

Example 1 is basically the same as the example created in my earlier post, but cleaned up a little. In this example the skin pulls data from the component.

Example 2 pushes the data from the component into the skin. You’ll see there’s a bit of added complexity in the component now, but the skin is much simpler. I believe this is a better approach and allows developers to use the skin in their own applications much easier.

Both examples have view source enabled, and you can grab also grab the source at github.

git://github.com/andymcintosh/SparkComponents.git

This entry was posted in Flex 4, Spark. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

7 Comments

  1. Justin Brown
    Posted August 12, 2009 at 8:43 pm | Permalink

    Hey Andy,

    Thanks for the great example. May I ask what version of the sdk you used to compile it? It doesn’t seem to work when I compile with the version that comes with Flash Builder Beta.

    Thanks,
    Justin

  2. Posted August 14, 2009 at 5:59 am | Permalink

    Hi There,

    I’m trying your component (on flex 4.0.0.9237) and get this error message:
    ‘1119: Access of possibly undefined property textView through a reference with static type AHTextInput. AHTextInput.as TestWeb/src line 29 Flex Problem’

    can you help me with this?

  3. Posted September 8, 2009 at 8:08 am | Permalink

    I used the version that came with the SDK. I was never able to successfully get any of the nightly builds working.

  4. Posted September 8, 2009 at 8:10 am | Permalink

    The Flex team has renamed a bunch of things in the SDK. textView is now called textDisplay or something.

    http://opensource.adobe.com/wiki/display/flexsdk/Rename+List

  5. Russell
    Posted September 17, 2009 at 2:01 pm | Permalink

    andy, you are right, they change lots of name and make me confused many times. Thank you for sharing the link.

  6. Ariel Jakobovits
    Posted March 3, 2010 at 11:56 am | Permalink

    nice post. good example of a common design issue in flex 4. one question: where is spark.primitives.SimpleText??

  7. Posted April 21, 2010 at 8:47 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">