Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tomoTaka01
Created April 13, 2014 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoTaka01/10571034 to your computer and use it in GitHub Desktop.
Save tomoTaka01/10571034 to your computer and use it in GitHub Desktop.
Flex closure sample
import flash.events.MouseEvent;
import mx.controls.Alert;
private function init():void {
this.result.text = "0";
this.add1Button.addEventListener(MouseEvent.CLICK, add(1));
this.add2Button.addEventListener(MouseEvent.CLICK, addWithObject(2, this));
this.addDiv.addEventListener(MouseEvent.CLICK, addHistory);
}
private function add(addVal:int):Function {
var i:int = 0;
return function() {
// Alert.show(this.toString());
i += addVal;
// this does not work because this has object global
// this.result.text = String(ix);
setValue(i);
}
}
private function setValue(ix:int):void {
this.result.text = String(ix);
}
private function addWithObject(addVal:int, objThis:Object):Function {
var i:int = 0;
return function(e:MouseEvent):void {
i += addVal;
objThis.result.text = String(i);
}
}
private function addHistory(event:MouseEvent):void {
var tmp:String = this.history.text;
this.history.text = tmp + event.target.id + "\n";
}
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="100" minHeight="100"
creationComplete="init()"
>
<s:layout>
<s:VerticalLayout gap="10" paddingTop="10" paddingLeft="10" />
</s:layout>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
#result {
color:red;
font-weight: bold;
}
</fx:Style>
<fx:Script source="includes/Adder.as" />
<fx:Declarations>
</fx:Declarations>
<s:Label text="Adder Sample(action script)" fontSize="14" color="blue" />
<s:HGroup gap="5">
<s:Label text="value:" />
<s:Label id="result" />
</s:HGroup>
<s:HGroup gap="5" id="addDiv">
<s:Button id="add1Button" label="add1" />
<s:Button id="add2Button" label="add2" />
</s:HGroup>
<s:VGroup paddingTop="10">
<s:Label text="click history"/>
<s:TextArea id="history" height="100" />
</s:VGroup>
</s:Application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment