Presentation Model Interface with Swiz

Lately I have started to use the presentation model approach and I like it a lot. Swiz fellow Ben Clinkinbeard has a wonderful example posted on his blog.

However I disliked that I have to dispatch custom events to make getters bindable. This would normally look like this in an interface:

[Bindable(event="customDataChanged")]
function get someData():Array;

The problem with it is that I have to dispatch an event typed “customDataChanged” from the presentation model implementation when I want the binding to be updated.
So my implementation would look like this:

private var _someData:Array;
public function get someData():Array{
    return _someData;
}
 
public function setSomeData(a:Array):void{
    _someData = a;
    dispatchEvent(new Event("customDataChanged"));
}

But what I wanted to use is the Swiz autowire-by-property style which would look like this:

[Bindable]
[Autowire(bean="appModel", property="someData")]
public var someData:Array

So for the examle above I have a bean with id appModel defined in my IoC Container (BeanLoader) which contains the someData variable. Swiz wires the property into the presentation model implementation but how do I get my interface getter bindable?
The solution is quite easy. Just have a look at the generated actionscript with the compiler args -keep-generated-actionscript and you see that by default a PropertyChangeEvent is dispatched for bindable variables when they are changed. The type value of the event is “propertyChange” so the only thing I have to change in my interface is the event type:

[Bindable(event="propertyChange")]
function get someData():Array;

So my view gets the presentation model interface autowired-by-type:

[Bindable]
[Autowire]
public var model:IMyPresentationModel;
<s:List dataProvider="{model.someData}"/>

Now when the application model someData property changes, Swiz updates the autowired property which fires a PropertyChangeEvent and forces the binding to be updated in the view.

2 comments to Presentation Model Interface with Swiz

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">