How to handle states

Terry's Avatar


19 Nov, 2009 07:32 PM

in the onRegister function I would like to map a listener to a button that is in a different view state than the currentState. This causes a run time error because the view injected into the mediator has not instantiated that button yet.

My current solution is to make a function for each view state in my mediator. Each function would be responsible for changing the view.currentState and then mapping the listeners.

  1. Support Staff 1 Posted by Shaun Smith on 19 Nov, 2009 07:38 PM

    Shaun Smith's Avatar

    In general, you should try to avoid digging into objects (ie
    view.childView.button.addListener) as this creates a very tight
    "structural" coupling. It might be better to listen directly to the
    view for custom bubbling events that you fire manually from the nested

  2. Support Staff 2 Posted by Joel Hooks on 19 Nov, 2009 07:42 PM

    Joel Hooks's Avatar

    Or, if you are like me and avoid bubbling like the plague, assign the click handler of the button to a method on the view that dispatches the custom event.

  3. 3 Posted by terry on 19 Nov, 2009 08:36 PM

    terry's Avatar

    I have opted to create an mx:Model object in the view and when the wizard is complete, I dispatch an event and pass that object into the mediator so I can transfer all the values into the application model.

  4. 4 Posted by Terry on 20 Nov, 2009 03:41 AM

    Terry's Avatar

    after many different attempts to wire my app in different ways, I really think I am starting to pick up how robot legs is supposed to work. I am just struggling with when to use the framework and when to just write some code. For instance, I need to control the flow of my view states, do I make a model and commands to to do this? or do I control the flow of my states using local code in the view? Which case makes you guys cringe the most? Should I think of everything my app does as a command?

  5. Support Staff 5 Posted by Joel Hooks on 20 Nov, 2009 03:53 AM

    Joel Hooks's Avatar

    A complex component that utilizes view states can manage those states internally. This sort of business really isn't the responsibility of the framework. Commands do not define everything the application does, they define interaction between the layers, or tiers, of your application. A user clicks a button, that updates some data. This triggers an event that executes a command that delivers that data to a Model class.

    Does that help at all?

  6. 6 Posted by Terry on 20 Nov, 2009 04:02 AM

    Terry's Avatar

    Yes, I will now print that paragraph and tape it on my monitor for rest of this application build :)

  7. Support Staff 7 Posted by Shaun Smith on 20 Nov, 2009 04:43 PM

    Shaun Smith's Avatar

    Haha :) Just to re-iterate:

    Generally speaking, view components should manage their own state as
    much as possible - and that state should have very little to do with
    the "application" state. Model classes manage application state, and
    Mediators translate changes in that state into calls that view
    components can understand. The view components should pretty much be
    oblivious to the application they happen to find themselves in.

  8. Support Staff 8 Posted by Joel Hooks on 20 Nov, 2009 08:23 PM

    Joel Hooks's Avatar

    There is a distinct separation between the visual state of an application and the state of the underlying data. Of course, the state of the data affects the visual representation of the components, but in terms of the visual user experience (states, transitions, and other goodies Flex provides us) they are managed by the component in response to the state of the data. Flex, as a framework in and of itself, provides a robust toolkit for managing state inside view components, and this should definitely be leveraged in our Flex applications.

  9. Joel Hooks closed this discussion on 20 Nov, 2009 08:23 PM.

  10. Terry re-opened this discussion on 23 Nov, 2009 05:01 AM

  11. 9 Posted by Terry on 23 Nov, 2009 05:01 AM

    Terry's Avatar

    After taking a break from it and coming back to my project I immediately facepalmed and instantly went to work fixing everything. I did get hung up for about an hour because I forgot to override clone() for one of my events. Once I decided that the view is the view and it should handle view things, everything started to come together for me.

  12. Support Staff 10 Posted by Shaun Smith on 23 Nov, 2009 06:05 PM

    Shaun Smith's Avatar


  13. Support Staff 11 Posted by creynders on 24 Nov, 2009 01:09 PM

    creynders's Avatar

    Just curious Joel, why do you avoid bubbling? Because it's very hard to find the originator of the event, ... or?
    I'm indecisive on whether or not to use event bubbling. When using pureMVC I used event bubbling to listen to custom 'VIEW_CREATED' events in my main mxml file, which just passed this on to it's mediator to let it create the corresponding mediators ( a very crude form of mediator mapping i guess) This was a huge timesaver because it was completely independent of the view hierarchy.
    But since I'm not using pureMVC anymore I'm doubting whether it's ok to use event bubbling...

  14. Support Staff 12 Posted by Joel Hooks on 24 Nov, 2009 02:22 PM

    Joel Hooks's Avatar

    Ya, I've been bit by the "wtf is the originator" and I am a bit of a control freak. I want to be as explicit as possible in my code. Having events race up and down the display list looking for listeners disturbs me. The bubbling you describe is actually the exact mechanism that the MediatorMap uses for automatic mediation (automatic mediation disturbs me a bit too ;) ).

  15. Support Staff 13 Posted by creynders on 24 Nov, 2009 04:11 PM

    creynders's Avatar

    Sound like you're just easily disturbed ;)
    No, I understand what you mean, there's something 'surely this'll go wrong' about it, however so far I haven't encountered any problems yet.

    Funnily I get the same shivers whenever I start debugging other people's cairngorm & pureMVC projects when it concerns events/notifications. Where is this coming from? Obviously if they strictly adhere to best practices you should be able to find out pretty easily where a notification originates from, but still...
    Decoupling means freedom which in turn means less control in some areas...

  16. Shaun Smith closed this discussion on 07 Dec, 2009 02:40 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac