Skip to content

Instantly share code, notes, and snippets.

@jmreidy
Created January 26, 2009 18:05
Show Gist options
  • Save jmreidy/52895 to your computer and use it in GitHub Desktop.
Save jmreidy/52895 to your computer and use it in GitHub Desktop.
package
{
import flash.events.IEventDispatcher;
import mx.binding.utils.BindingUtils;
import mx.events.FlexEvent;
[DefaultProperty("mappings")]
public class DIMap
{
private function addBindingListeners( event:FlexEvent ):void
{
for each ( var map:Mapping in mappings )
{
if ( event.target is map.target )
{
if ( map.targetId != null )
{
if ( map.targetId == event.target.id )
{
BindingUtils.bindProperty( event.target, map.targetProp, map.source, map.sourceProp );
}
else
{
continue;
}
}
else
{
BindingUtils.bindProperty( event.target, map.targetProp, map.source, map.sourceProp );
}
}
}
}
public var mappings:Array;
public function set target( value:IEventDispatcher ):void
{
value.addEventListener( FlexEvent.CREATION_COMPLETE, addBindingListeners, true, 0, true );
}
}
}
package
{
public class Mapping
{
public function Mapping()
{
}
public var source:Object;
public var sourceProp:String;
public var target:Class;
public var targetProp:String;
public var targetId:String;
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
xmlns:local="*">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Label;
import mx.controls.Button;
import mx.controls.Alert;
import mx.containers.HBox;
import flash.utils.getQualifiedClassName;
import mx.events.FlexEvent;
import flash.utils.getTimer;
private function changeModelValues( event:Event ):void
{
var model:Model = Model.instance;
model.buttonLabel = "new button label";
model.sampleContent = "content changed in model";
}
]]>
</mx:Script>
<local:DIMap target="{ testContainer }" >
<local:Mapping source="{ Model.instance }" sourceProp="buttonLabel" target="{ Button }" targetProp="label" />
<local:Mapping source="{ Model.instance }" sourceProp="sampleContent" target="{ TextInput }" targetProp="text" targetId="onlyThisTI" />
</local:DIMap>
<mx:VBox id="testContainer" height="100%" width="100%" >
<mx:Button width="100"/>
<mx:Button width="100" />
<mx:TextInput id="onlyThisTI" width="150" />
<mx:TextInput width="150" />
<local:TestBox />
</mx:VBox>
<mx:Button click="changeModelValues( event )" label="Change Model Values" />
</mx:Application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment