Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4f3e94e
Showing
7 changed files
with
444 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package | ||
{ | ||
import flash.text.TextField; | ||
import flash.display.Sprite; | ||
import crayons.data.binding.Binder; | ||
|
||
public class Example extends Sprite | ||
{ | ||
private var source:SomeModel; | ||
private var target:TextField; | ||
|
||
private var binder:Binder; | ||
|
||
public function Example() | ||
{ | ||
super(); | ||
|
||
// you only need one of these throughout your app | ||
binder = new Binder(); | ||
|
||
source = new SomeModel(); | ||
target = new TextField(); | ||
|
||
addChild(target); | ||
|
||
// this binds source.data to target.text, | ||
// provided source dispatches dataChanged when its data property changes | ||
binder.bind(source.dataChanged, target, "text"); | ||
|
||
// this should set the data in the target text field | ||
source.data = "Data Binding !"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package | ||
{ | ||
import crayons.signals.ChangeSignal; | ||
|
||
public class SomeModel | ||
{ | ||
private var _data:String; | ||
public var dataChanged:ChangeSignal; | ||
|
||
public function SomeModel() | ||
{ | ||
dataChanged = new ChangeSignal("data"); | ||
} | ||
|
||
public function get data():String | ||
{ | ||
return _data; | ||
} | ||
|
||
public function set data(value:String):void | ||
{ | ||
if(_data == value) | ||
return; | ||
|
||
var old:String = value; | ||
_data = value; | ||
// dispatch change signal whenever data changes | ||
dataChanged.dispatch(old, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package crayons.data.binding | ||
{ | ||
import crayons.signals.ChangeSignal; | ||
|
||
import flash.utils.Dictionary; | ||
|
||
|
||
|
||
public class Binder | ||
{ | ||
|
||
protected var bindings:Dictionary = null; | ||
protected var allowOverwrites:Boolean = false; | ||
protected var useWeakReferences:Boolean = false; | ||
|
||
|
||
|
||
public function Binder(allowOverwrites:Boolean = false, useWeakReferences:Boolean = false) | ||
{ | ||
this.bindings = new Dictionary(useWeakReferences); | ||
this.useWeakReferences = useWeakReferences; | ||
this.allowOverwrites = allowOverwrites; | ||
} | ||
|
||
|
||
|
||
public function bind(signal:ChangeSignal, host:*, property:String):void | ||
{ | ||
createBinding(signal, host, property); | ||
} | ||
|
||
|
||
|
||
public function hasBinding(host:*, property:String):Boolean | ||
{ | ||
if(bindings[host] == null) | ||
return false; | ||
|
||
var binding:Binding = bindings[host][property]; | ||
return binding != null && binding; | ||
} | ||
|
||
|
||
|
||
public function unbind(host:*, property:String):void | ||
{ | ||
if(hasBinding(host, property)) | ||
{ | ||
var binding:Binding = bindings[host][property]; | ||
|
||
binding.signal.remove(binding); | ||
binding = null; | ||
|
||
delete bindings[host][property]; | ||
} | ||
} | ||
|
||
|
||
|
||
public function unbindAll():void | ||
{ | ||
this.bindings = new Dictionary(useWeakReferences); | ||
} | ||
|
||
|
||
|
||
protected function createBinding(signal:ChangeSignal, host:*, property:String):void | ||
{ | ||
if(bindings[host] == null) | ||
{ | ||
bindings[host] = []; | ||
} | ||
|
||
if(bindings[host][property] != null && !allowOverwrites) | ||
{ | ||
throw new Error("Binding previously defined"); | ||
} | ||
else | ||
{ | ||
if(hasBinding(host, property)) | ||
{ | ||
unbind(host, property); | ||
} | ||
|
||
var binding:Binding = new Binding(signal, host, property); | ||
signal.add(binding); | ||
|
||
bindings[host][property] = binding; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package crayons.data.binding | ||
{ | ||
import crayons.signals.ChangeSignal; | ||
import crayons.signals.ChangeSlot; | ||
|
||
|
||
|
||
public class Binding implements ChangeSlot | ||
{ | ||
|
||
private var _signal:ChangeSignal = null; | ||
private var _host:*; | ||
private var _property:*; | ||
|
||
|
||
|
||
public function Binding(signal:ChangeSignal, host:*, property:*) | ||
{ | ||
_signal = signal; | ||
_host = host; | ||
_property = property; | ||
} | ||
|
||
|
||
|
||
public function get signal():ChangeSignal | ||
{ | ||
return _signal; | ||
} | ||
|
||
|
||
|
||
public function onChange(oldValue:*, newValue:*):void | ||
{ | ||
_host[_property] = newValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package crayons.signals | ||
{ | ||
|
||
|
||
|
||
public class ChangeFunctionSlot implements ChangeSlot | ||
{ | ||
|
||
public var func:Function; | ||
|
||
|
||
|
||
public function ChangeFunctionSlot(func:Function) | ||
{ | ||
this.func = func; | ||
} | ||
|
||
|
||
public function onChange(oldValue:*, newValue:*):void | ||
{ | ||
if(func != null) | ||
{ | ||
func(oldValue, newValue); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.