Archive for February 18th, 2009

AS3 Binding without the Flex Framework

Wednesday, February 18th, 2009 | examples | Comments

This example relies on the Flight Framework (included in the source) developed by my good friends Tyler, Rob, and Jacob. You can expect to see more on flight here in the future.

You can read more details about this custom binding here: http://www.xtyler.com/code/177

View demo
Download source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
 
	import flight.binding.Binding;
	import flight.events.PropertyEvent;
 
	[SWF(backgroundColor="#ffffff")]
	public class BindingTest extends Sprite
	{
		private var sourceBinding:Binding;
 
		public var sourceText:TextField = new TextField();
		public var targetText:TextField = new TextField();
 
		private var isTargetTextBound:Boolean = false;
 
		//as3 binding requires a getter/setter for the property you want to bind to
		private var _source:Number = 0;
		public function get source():Number
		{
			return _source;
		}
 
		public function set source(value:Number):void
		{
			var oldValue:Number = _source;
			_source = value;
			//you need to dispatch an event to indicate the binding source has changed
			PropertyEvent.dispatchChange(this, "source", oldValue, _source);
		}
 
		public function BindingTest()
		{
			//set up text for visual display
			var textFormat:TextFormat = new TextFormat("Arial", 20, 0x000000);
			sourceText.defaultTextFormat = textFormat;
			targetText.defaultTextFormat = textFormat;
 
			addChild(sourceText);
			targetText.y = 40;
			addChild(targetText);
 
			//setup source of binding
			sourceBinding = new Binding(this, "source");
			//bind to the first text's "text" property
			sourceBinding.bind(sourceText, "text");
 
			//update with timer			
			var timer:Timer = new Timer(100);
			timer.addEventListener(TimerEvent.TIMER, timer_timerHandler);
			timer.start();
 
			stage.addEventListener(MouseEvent.CLICK, stage_clickHandler);
		}
 
		private function timer_timerHandler(event:TimerEvent):void
		{
			//increment the source
			source++;
		}
 
		private function stage_clickHandler(event:MouseEvent):void
		{
			//bind or unbind to second textField on mouse click
			if(isTargetTextBound)
			{
				sourceBinding.unbind(targetText, "text");
			}
			else
			{
				sourceBinding.bind(targetText, "text");
			}
 
			isTargetTextBound = !isTargetTextBound;
		}
	}
}

Tags:

Search

Recommended Books

Speaking at FITC Toronto

Recent Comments

 

February 2009
M T W T F S S
« Jan   Mar »
 1
2345678
9101112131415
16171819202122
232425262728  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...