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.

7 comments to Presentation Model Interface with Swiz

  • Fred

    Thanks for the tip, very useful!

  • [...] As an inspiration and some guides I used one of Ben Clinkinbeard blog posts and also Soenke Rohde blog post. With my approach I introduce assumption that all/most PresentationModel classes extend [...]

  • Des Hartman

    Sonke,

    Could you provide the final presenter interface & implementation? I am trying to follow your example, but not sure if you have

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

    in the implementation and

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

    in the interface of the presenter?

    Thanks
    Des

  • The inferface only contains this:
    [Bindable(event="propertyChange")]
    function get someData():Array;

    Does this answer your question?

  • Des Hartman

    Sonke,

    So I would have the following?

    Interface
    =========

    public interface ITest{

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

    function set someDate(value:Object);
    }

    Implementation
    ==============

    public class Test implements ITest{

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

    }

  • Yes, and you can eve remove the setter in the interface when it should be read-only.

  • Des Hartman

    Thanks
    Des