1

ok so I have a TitleWindow that I open up... and I have 6 states defined. I am using the Presentation model pattern for all of my views. I have discovered a frustrating nuance. When I tell my window to go to XXX state, the controls have to initialize since the states in flex use lazy loading. so... my PM code that says myTextArea.text bombs out and say "cannot access..." so as a work around, I made some creationComplete events on my controls to register the control with the PM. So when the state changes, the textarea finally initializes and on creationComplete calls PM.registerTextArea() which sets a reference to it. then in that function I run my code... myTextArea.text.. etc.

This seems like such a ugly hack that I hate it. Is there any way to wait until the entire state in created then call code on the PM? I have tried enterstate... but this event seems to fire before the state controls are ready.

I tried to add a comment, but I guess editing is the only way I can do this...

to everyone: thanks for the great feedback. I am doing something slightly off straight PM. Every view has a viewController (as I call them). Its kinda my own hybrid of a delegate / dataprovider. But this is moot. It's the flex component lifecycle when dealing with states that is the pain. if you change state.. there is no event to say "all my components in this state are ready". only event to say "we changed state". so my code that fires on state change is trying to talk to components that aren't ready yet. So from what I see, there seems to be no design pattern or perfect way to ensure that all components are created in a state unless using creationComplete on every component in the state to register it is ready... if you don't, you get a race condition. Regardless of frameworks or design patterns, this seems to be a root issue.

The textarea is an easy PM fix.. just bind it to the pm value. But there are other times I can't.

Specifically, I am trying to attach video to a display once I get to that state. This is done via addchild. regardless of where I do it.. I need to know that the videoDisplay is done loading before I call addchild. I even tried currentStateChange event since docs say that fires last... but alas.. the components in the state are still initializing. So it seems that creationComplete is my only option. Maybe the only sane way to keep to clean code is to create the entire thing (videodisplay and video) using as once the state is entered. I just hoped the flex framework had events to ehlp me here rather than buiilding everything on the fly in as.

Jason Reeves
  • 1,716
  • 1
  • 10
  • 13
  • You may not like this either since it's getting a bit dirty, but what about just creating a public bindable variable within the view class for holding the value that should be displayed in the textarea? This way you push from the controller onto the presentation via the interface but the view actually uses an internal "buffer" to hold the value until the control is created and binds to it? – shaunhusain Jul 13 '11 at 05:05
  • Also what's your motivation for using MVP, I've tried to understand it a few times but have never really implemented it because I don't see the advantage over MVC aside from possibly easier to test and slightly more decoupled, but never seemed worth it for the extra complexity. Just out of curiosity whats the motivation? – shaunhusain Jul 13 '11 at 05:25
  • @shaunhusain: MVP != Presentation Model . I like Presentation Model over MVC because it is less complicated, highly decoupled and provides a great abstraction of the view in a testable way. – Brian Genisio Jul 13 '11 at 10:11
  • I think you need to show us some code that illustrates the problem. – Brian Genisio Jul 13 '11 at 10:13
  • @Brian Genisio, that statement is debatable. I've often related MVP to model-view-presenter (presentation model, or presenter first). This is semantics. @Jason, please post the code. I think you're doing something off in the PM/context. – J_A_X Jul 13 '11 at 12:21

1 Answers1

0

Since your PM has a reference to a visual component (myTextArea), this isn't a completely pure form of a presentation model. It appears to be more of a supervising presenter / controller type of setup.

That being said, the way I would fix your problem would be to use a complete presenter outright ( no knowlege of the view at all ) or use a complete controller ( view implements an interface through which the controller communicates ). The advantage of using a presenter in Flex is that you can create a bindable value such as text or selectedItem, and the view will bind to that variable whenever it comes online so the issues dealing with the lifecycle of Flex components go away.

Black Dynamite
  • 4,067
  • 5
  • 40
  • 75
  • I know this was an old question, but you were kinda right here. We were retrofitting a PM pattern in an existing app and it ended up being square peg / round hole. In the end we went with IOC dependency injection.... (and rewrote the thing). Just got active again on here wanting to give back a little and saw I never gave you credit for an answer that was a good one. – Jason Reeves Nov 29 '12 at 02:30
  • @JasonReeves Yeah, bad architecture in Flex can really hurt you in the long term. My first contract involved me working with code that had 13 different singletons each referenced 70 times in the application in all layers. Nevertheless, the project didn't go too far. IOC helps facilitate testing and forces you to think about separation of concern and responsibility from the get-go. – Black Dynamite Dec 01 '12 at 01:42