Avg. Rating 3.5

Problem

Common problem with static classes - they "ignore" binding mechanism.

Solution

Create (or access via refference) instance as app public variable and bind it/to using <mx:Binding> (or <s:Binding> for SDK 4)

Detailed explanation

 So, common problem is developer has some static class or singleton, for exaple named "NodeSystemModelLocator" with public public property (implemented via getter/setter) "selectedGene". This class implements model locator pattern for some application. Developer should bind mentioned property to some interface variable (some view controller property). In this case solution has several steps:

 

1) Add [Bindable] to model static class declaration:

 

[Bindable]   

public class NodeSystemModelLocator extends EventDispatcher{

private static var modelLocator : NodeSystemModelLocator;

public static function getInstance() : NodeSystemModelLocator

   {

   if ( modelLocator == null )          

     {              

         modelLocator = new NodeSystemModelLocator();

       }

      return modelLocator;

       }

  private var _selectedGene : GeneVO;

  public function get selectedGene():GeneVO{

            return _selectedGene;

       }

  public function set selectedGene(gene:GeneVO):void{

              _selectedGene = gene;

             //some code

       }

}

 

2) in main app add variable to store singleton instance:

<mx:Script>

     <![CDATA[

//some code

   [Bindable]

   public var _model : NodeSystemModelLocator;

    ]]>

</mx:Script>

 

3) in main app add mxml binding:

 

<mx:Binding destination="navView.deleteGeneCB.selectedItem" source="_model.selectedGene"/>

 

navView instance declared in main app layout as

<view:NodeSystemNavigationView top="0" right="0" id="navView"/>

and deleteGeneCB is a combo box instance id there.

 

 So, now we have 2-way binding for model (singleton) instance variable and visual component property. And it works properly.

 

Report abuse

Related recipes