Home > Flex > Getting a bunch of components that work together to know about each other

Getting a bunch of components that work together to know about each other

July 9th, 2006

I’ve been working on a fairly complex piece of user interface that has a number of UIComponent subclasses that are nested to form a single cohesive whole. As the UI has gotten more complex, increasingly an object would need to be able to ask a descendant something, and a child would need to ask an ancestor something. (I’m using “descendant” and “ancestor” in the sense of containment, not of inheritance.)

This is generally fine from a philosophical point of view: it is not a goal that these subcomponents should be able to live outside of this hierarchy. And it’s fine from a mechanical point of view — after all the objects have been created, that is. But at initialization time, not all the objects will have been created yet, and I realized I didn’t have a good set of rules to follow for making sure that objects would always be able to get to their children or parents when they needed them.

I ended up standardizing on the following four rules, based on the fact that objects generally listen for the FlexEvent.CREATION_COMPLETE event, at which point all their children are created and it is safe to access them.

1) Any child that needs to ask an ancestor something has to provide a setter that the top-level component can set with its this pointer, so that the child ends up with a pointer back to the top-level component.

2) For every descendant component, the top-level component calls this setter in its CREATION_COMPLETE handler.

3) The top-level component has getters (or public members) corresponding to each descendant which some descendant needs access to.

4) Any time a descendant needs access to an ancestor (which may not yet have been created) during initialization, its first access of that ancestor must be in the setter mentioned in #1 above.

This has worked out pretty well.

Technorati Tags: ,

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • StumbleUpon
Author: David Coletta Categories: Flex Tags:
  1. July 12th, 2006 at 17:06 | #1

    Yo, can you show some psedo code of this? It doesn’t have to be strongly-typed, just a basic implementation? Pretty please?

  2. david
    July 13th, 2006 at 09:20 | #2

    More here.

  3. August 19th, 2006 at 05:47 | #3

    This is pretty interesting,

    I’m not sure if I’m right in thinking this, but are there problems of this nature when using MXML to construct a UI, – certain components are not accessible thru their object references until they have been viewed once.

    For example a component inside a TabNavigator (and the like) will not be registered as a valid object reference until it has been viewed once, which is awkward if you need to dynamically populate the component(s).

    I’m wondering if there is any way to force all (or more usefully specific) components to reach their CREATION_COMPLETE state and therefore register their object references into the Flex object model at a given stage in execution?

    At the moment I’m working my way around the problem by casting a variable reference. For example to access a DataGrid (called dataGridOne) in a Canvas (called canvasTwo) in TabNavigator (called tabNavOne) I’d have to do the following… (assuming TabNavigatorOne is already constructed and canvasTwo has not yet been accessed / constructed.)

    var dataGridOne_ref : DataGrid = DataGrid(Canvas(tabNavOne.getChildByName("canvasTwo")).getChildByName("dataGridOne"));

    Which although workable, seems pretty ____ nasty to me.

    Can you she any light on this?

  4. August 19th, 2006 at 05:53 | #4

    (man I hate it when I typo! … that was “shed and light on this” … not “she light on this” of course.)

  5. August 19th, 2006 at 05:54 | #5

    (*&@$! !!)

  6. August 19th, 2006 at 06:14 | #6

    My mistake, it seems my testing of that method of accessing an unconstructed component only reaches one level down, so I could access canvasTwo but not the children of canvasTwo …

    So…

    Can you shed any light on this?

  7. August 19th, 2006 at 06:17 | #7
Comments are closed.