2 Replies Latest reply on Sep 29, 2008 10:55 AM by Newsgroup_User

    Flex FAQ 9-29-08

    Level 7
      Amy's Flex Frequently Asked Questions
      Q: I want to use an XML data source to dynamically display images and
      data in my Flex application. I won't know how many images/data points I
      will have at design time. How can I best approach this?

      A: Depending on what you're wanting to do, you can either use a
      Repeater with an Image or custom component that contains an Image or you can
      use a List based component to display an Image or custom component with an
      Image (or SwfLoader, or Loader) in it. The Help is full of resources on
      this subject. Here are some places you may want to try:

      Help>Flex Start Page>Creating a Simple RIA
      Help>Help Contents>User Interfaces
      - Using Data Providers and Collections
      - Controls
      - Image
      - SwfLoader
      - Using Data Driven Controls
      - Using Item Renderers and Item Editors
      - Dynamically Repeating Controls and Containers

      Here is an example that might get you running fast if you find it easier to
      learn from a working example than from Help files:

      Q: I've created a custom itemRenderer component to use in a List
      based component (Datagrid, TileList, HorizontalList, etc.). When my List
      first displays, everything looks fine, but when I scroll it or change the
      dataProvider, some of the itemRenderers show values or formatting that
      aren't right. How do I fix this?

      A: List-based components don't draw a renderer for every item in the
      dataProvider. Instead, they create enough to display what is on screen now,
      plus one or two more waiting in the wings. This means they recycle the
      renderers rather than creating new ones when you change dataProvider or
      scroll up and down. When you use a creationComplete event to set up the
      itemRenderer, that event doesn't happen again when the renderer is used for
      a different set of data. The solution to this is to override the set data
      protected function that most components have.

      For more information, check out the following resources:

      Q: I want to run a function in my main application from inside my
      custom component. But when I try to refer to myFunction() in that
      component, I get a compile time error Call to a possibly undefined function
      myFunction. How can I fix this?

      A: Your component has its own scope, so it doesn't know anything
      about the functions in the main file. You can get around this by directly
      referencing the main application scope like this:
      Application.application.myFunction(). However, this makes your component
      tightly coupled, which is a quick way of saying that your component is only
      usable in an application that has a myFunction() function in it. You're
      better off dispatching an event from your component and letting the
      application decide how to handle it. For more information, check out the
      following resources:

      Q: I want to decide at runtime what kind of component to add to
      display my data. How can I accomplish this?

      A: Use getDefinitionByName and ClassFactory to dynamically create
      your class. Just be sure that you actually have at least one "hard"
      reference to each class you intend to use, or the class might not get
      compiled into your swf. Here's a full write-up of how to do that:

      Q: I need to set a property or add an event listener on a component
      that is in a ViewStack/TabNavigator/Accordion. When the component is not
      the first child of the Navigator Container, I get a null object error
      (#1009). What causes this, and how can I fix it?

      A: By default, the Navigator containers only create the children of
      each pane as that pane is viewed. The easy way to fix this is to set the
      creationPolicy on the Navigator to "all." However, this will cause your
      application to take longer to load. A better way to fix this is to wait for
      a later event, such as creationComplete on the component you want to access,
      or to use binding to "pull" the data into the component.
      The way I handle it is to call invalidateProperties() on change of the
      ViewStack. I then override commitProperties() and call an "initializer" for
      each pane. In the body of each initializer function, I check to see if the
      selectedItem for the viewStack is the one my initalizer cares about. If
      not, I return from the function immediately. Inside that initializer
      function, I set properties and add listeners as appropriate.

      Q: When my application is taller than the browser window, I get
      scrollbars in the application. I want the browser to do the scrolling. How
      can I do this?

      A: In the html-template/index.template.html file change line 46 (in
      the Flex 3 template) to read <body scroll="auto"> instead of the default of
      <body scroll="no">.

      Q: I am using URLRequest, HTTPRequest, or HTTPService to retrieve
      information from a database on a server. The first time my application
      makes a server call, it works fine. But when I call the same page again, I
      get the same data, even though I know the data has changed on the server.
      Is this a bug?

      A: The browser has cached the response it got from the first call
      you made to the page. This means that you need to either change how you are
      calling the page so it appears to be a new request, or you need to change
      the page itself so that it won't be cached.

      To change how you are calling the page, append a unique value to the end of
      the URL. For example, new URLRequest('yourPage.asp?param="+(new

      If you want to change the page itself, you need to change the response
      headers. The method for changing the response headers will vary from
      language to language. For more on this see:


      Q: I am using Modules in my Application. When I load the first
      Module, everything works fine. But when I load the second one, I get a Type
      Coercion Error failed error #1034 that looks something like TypeError: Error
      #1034: Type Coercion failed: cannot convert
      com.myDomain.package::SingletonClass@fce9a89 to
      com.myDomain.package::SingletonClass or TypeError: Error #1034: Type
      Coercion failed: cannot convert mx.managers::HistoryManagerImpl@22346589 to
      mx.managers.IHistoryManager. How do I fix this?

      A: When you reference a singleton class, either a custom singleton
      or one of the managers in Flex, into a Module, if this is not loaded in the
      Application domain, it will load into the child domain of the first Module
      to load and other Modules will not be able to see it. To prevent this,
      either directly load your class into the main Application before loading any
      Module, or use a Runtime Shared Library with the class included.

      For more on this, see Alex Harui's presentation on Modules from Flex 360
      http://blogs.adobe.com/aharui/2007/03/modules.html (slides 19, 24-31).

      Not sure what a Singleton is? Check out
      http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&postId=262&produc tId=2

      Q: I'm using a Repeater to lay out the right number of components on
      the screen for me based on a data source. I'm trying to set up the id for
      each component dynamically like this:

      <mx:Repeater id="myRepeater" dataProvider="mySource">
      <mx:Button id="{'myButton'+myRepeater.currentIndex}"
      label="{myRepeater.currentItem.label}" />

      When I try to run the file, I get a compiler error
      '{'myButton'+myRepeater.currentIndex}' is not a valid identifier.

      I need to be able to reference each of the repeated components. How can I
      do this?

      A: If you give the component an ordinary id like this:
      <mx:Button id="myButton" label="{myRepeater.currentItem.label}" />
      Flex will create an Array for you called myButton that contains a reference
      to each component the repeater created. For more information, see
      Referencing Repeated Components here:

      Courtesy of Amy's Flex Diary ( http://flexdiary.blogspot.com).

      The most recent version of this FAQ can always be found at