开发者

Databinding from the Skin to the View in Flex (Spark)

I'm pretty new to Flex development in Spark and wanted to clarify the best way to build components.

I had previously tried to use a binding expression to set the selected index of a ViewStack:

public class MyComponentView extends SkinnableComponent
{
    public var selectedIndex:int = 0;

}

<s:Skin ...>
    <mx:ViewStack selectedIndex="{hostComponent.selectedIndex}">
            ....
    </mx:ViewStack>
</s:Skin ...>

However, this binding expression doesn't appear to work, although it does show the correct number if I move that binding expression to a s:Label.

In order to get this to work I changed the code thus:

public class MyComponentView extends SkinnableComponent
{
    [SkinPart(required = "true")]
    public var myStack:ViewStack;

    private var _selectedIndex:int = 0;

    private var _indexChanged:Boolean;

    public function set selectedHistoryIndex(value:int):void
    {
        _selectedIndex = value;
        _indexChanged = true;
        invalidateProperties();
    }

    override protected function partAdded(partName:String, instance:Object):void
    {
        super.partAdded(partName, instance);

        switch (instance)
        {
            case myStack:
                _indexChanged = true;
                invalidateProperties();
                break;
        }
    }

    override protected function commitProperties():void
    {
        super.commitProperties();

        if (_indexChanged && myStack)
        {
   开发者_如何学C         _indexChanged = false;
            myStack.selectedIndex = _selectedIndex;
        }
    }

}

<s:Skin ...>
    <mx:ViewStack id="myStack">
            ....
    </mx:ViewStack>
</s:Skin ...>

Is this the way I'm meant to do it?


As for me, your second way is more preferable. I'd rather change some code to make it better:

public function set selectedHistoryIndex(value:int):void
{
    if (_selectedIndex == value)
        return;
    _selectedIndex = value;
    _indexChanged = true;
    invalidateProperties();
}

Yes, you can bind to component's properties from skin but this way View (skins in Spark architecture is for View in MVC and host component is for M and C) has knowledge about M which isn't good. The first implementation requires this knowledge from skin.

The second implementation makes View true View (managed by M). And it is good.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜