tags:

views:

45

answers:

2

Hi all, I'm quite new to the MVC design-pattern, and I'm translating all of my old code.

I want to move to this pattern because I can change my views according to my needs, but I'm finding difficult to do it at runtime.

I found an excellent example of MVC, and all that I do is the following:

<mx:ViewStack xmlns:mx="http://www.adobe.com/2006/mxml" resizeToContent="true" xmlns:views="MVC.views.*">
  <views:HomeView id="Home"/>
  <views:SecondPage id="SecondPage "/>
</mx:ViewStack>

what if I wanted to change (whenever the Controller says so) one of the views, for example the SecondPage view?

(Hope I made my point clear)

A: 

Use some MVC framework like Cairngorm or Mate to begin with

Besides that you can have a model which maintains state of the application and bind selected index of ViewStack to it.

Nishu
yes, that's pretty much what I did, I added another view to the viewstack, and set it like the current one.. this deadline is coming up, so I don't really have time to do anything else.
Alberto M
A: 

In Flex it is generally considered good practice to avoid holding direct references to your view components in your controller layer. The most common way to update views is by data binding. MATE is, in my opinion, the best Flex framework for this because it makes it very easy to inject data into views in a loosely coupled way.

However, if you actually bind to a variable holding a selectedIndex for your view stack, you are putting presentational knowledge into your model, which is also undesirable. An alternative solution is to set-up event handlers in your views that respond to events occurring elsewhere in your application and update themselves. MATE has ListenerInjectors which you can use in an event map to wire the view to the event.

lach
I'll try MATE for my next project, thanks.What do you mean with "presentational knowledge into your model?"The way I'm working now is that I set different variables (arrays, booleans, etc) in my model.. I make them Bindable, and then the Controller change the values according to my needs. Other times I dispatch events that change those values.. but it's kinda becoming not really usable..
Alberto M
You're doing it the right way. So long as those bindable variables represent your domain knowledge then they belong in the model. However, if you start adding properties to your model which are storing view states (like the selectedIndex on a view stack), then you are polluting your model with view concerns. Flex seems to encourage this. But if it starts to get out of hand, consider injecting event handlers in the view instead. Also, don't be afraid to break up your model locator into more focused classes if its starting to bloat.
lach