1

I'm refactoring some Flex code written by another developer and I'm implementing a PresentationModel approach, as I like to separate out the ActionScript from the MXML. One of the problems I've found is that the original ActionScript code adds/removes elements from the MXML.

What happens is the handler function checks the model and if the values are correct will either create or remove a element from the view. What is the best way to get the presentation model to ad elements to the view and still keep this loose coupling that I'm aiming for.

I was thinking of using simple events which the presentation model dispatches and a view can list for passing the details of the element to add. Is there another solution?

Thanks

Stephen

StephenAdams
  • 521
  • 2
  • 9
  • 26

2 Answers2

1

If you're using the presentation model, I'd assume that you have some kind of data of what needs to happen. When items of any sort are being dynamically added/removed, I make sure to make it data-driven for easier manipulation. If you want another item added, add another data model to your dataProvider of choice (List, ComboBox, DataGroup, etc).

By doing this approach, you're abstracting the logic from the presenter to the view. Events should only be used as a way for your view to know when your presenter has accomplished something. Data can be received this way (and it's good practice to do so) OR you can just bind the data within the presenter to your dataProvider. Both are good, I just find binding to be cleaner and simpler.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • Well the original code, remember I'm refactoring existing code here, has a setter function which sets a value then calls another function called drawBorder(). This drawBorder function creates a border shape and adds this using addElement(). – StephenAdams Apr 11 '11 at 16:21
  • yes, I know you're refactoring. If you're going to refactor to use the Presenter pattern, you're going to have to do some changes. It's just the way coding works. – J_A_X Apr 11 '11 at 16:37
0

Every part of code that do some graphical stuff (drawing border, setting style, drag & drop management, animations, ...), should be included in the view, not the presentation model.

For this kind of graphical that stuff should be executed after a property has been changed in the PM, we use the Cairngorm 3 Observer lib. Basically, it listens to some changes in the presentation model and allows you to execute a function in the View.

<cg:ObserveValue 
    source="{ model.firstName }" value="{ Name.SARA }"  
    handler="runEffectFunction"/>

See the documentation

Florian F
  • 8,822
  • 4
  • 37
  • 50
  • I've looked at the Observer library, but I was thinking that Data Binding my by a solution. I'm going to set a show/hide flag in my PresentationModel. Then bind this to the visible property of the elements I'm trying to add. This way I can change the show/hide flag and switch on/off the elements instead of trying to add them, which I can't do the current way this page has been written. – StephenAdams Apr 15 '11 at 09:24