0 Replies Latest reply on Sep 11, 2008 9:40 AM by Newsgroup_User

    Flex FAQ

    Level 7
      I've been compiling a FAQ of questions I see asked a lot in different
      formats. I'm posting it here for the convenience of the newsgroup users.

      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:
      http://examples.adobe.com/flex2/inproduct/sdk/photoviewer/PhotoViewer.html



      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:
      http://www.adobe.com/devnet/flex/articles/itemrenderers_pt1.html?devcon=f1
      http://blogs.adobe.com/aharui/2007/03/thinking_about_item_renderers_1.html



      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:
      http://www.adobe.com/devnet/flex/articles/loose_coupling.html
      http://www.adobe.com/devnet/flex/articles/graduating_pt1.html



      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:
      http://www.paulofierro.com/archives/520/



      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 ~77 to
      read <body scroll="auto"> instead of the default which turns it off
      explicitly.



      Brought to you courtesy of http://flexdiary.blogspot.com