tags:

views:

58

answers:

2

Hi,

I'm creating a MVC base application, in the past I've alwaysed use Cairngorm as the framework for my Flex applications. For this app I'm experimenting with other approaches to mvc, not other frameworks (pureMVC or Mate), but writing my own MVC base application.

My question is in Cairngorm I've always created the model as a singleton, but what ways can I pass data from the model to the view and not use a singleton.

I was thinking of injecting the model into views or is another approach sending events containing data to and from the model to the view via the controller?

Thanks

Stephen

+3  A: 

Personally, I think the easiest way to deal with this situation is to inject the Model directly into the View (via a constructor, or other mechanism).

Simple, yet effective.

Justin Niessner
Indeed, or with a Ioc container like spring.actionscript. We also use a singleton just because it's easy, but it's not best-practice.
Lieven Cardoen
How would you inject a model class into a MXML based view? Using public properties?
stevo
With properties, with (optional) constructor args. Or, PureMVC uses an external Mediator class which handles all updates of the view.
Michael Brewer-Davis
In Mate, you'd inject the model into a property on the view using the Injector tag that's implemented by the framework. Search for "presentation model" on the Mate forum to learn more about a powerful pattern that may be of interest to you.
Stiggler
A: 

I would declare the data as public properties. You almost always need to update data in a View based on user gestures, so using constructor arguments alone isn't very flexible and can be problematic for MXML-based Views.

Then you can either use binding expressions in the parent View to supply the data or use an IoC framework such as Swiz or Mate to inject the data. The disadvantage to the former approach is that you end up putting a lot of public properties in your parent views just so they can "relay" data to the child views. The nice thing about IoC is that you can add only the properties each View actually uses and then inject the data only where it's really needed.

cliff.meyers