diff --git a/samples/userInfo/build.hxml b/samples/userInfo/build.hxml new file mode 100644 index 0000000..579d763 --- /dev/null +++ b/samples/userInfo/build.hxml @@ -0,0 +1,5 @@ +-main Main +-cp src +#-lib bindx2 +-cp ../../src +--interp \ No newline at end of file diff --git a/samples/userInfo/src/Main.hx b/samples/userInfo/src/Main.hx new file mode 100644 index 0000000..c518ec7 --- /dev/null +++ b/samples/userInfo/src/Main.hx @@ -0,0 +1,27 @@ +package ; + +class Main { + static function main() { + var user = new UserModel("deep", 100); + var view = new UserView(); + view.user = user; + //> TextField::text changed to: Hello deep. You have 100 coins + //> user health changed from null to 1 + + user.coins = 200; //> TextField::text changed to: Hello deep. You have 200 coins + + //user.health = 0.5; //> user health changed from 1 to 0.5 + + view.user = null; + + user.name = "profelis"; + + view.user = user; + //> TextField::text changed to: Hello profelis. You have 200 coins + //> user health changed from null to 1 + + view.user.coins = 300; //> TextField::text changed to: Hello profelis. You have 300 coins + + user.health = 0.75; //> user health changed from 1 to 0.75 + } +} \ No newline at end of file diff --git a/samples/userInfo/src/UserModel.hx b/samples/userInfo/src/UserModel.hx new file mode 100644 index 0000000..5375d2d --- /dev/null +++ b/samples/userInfo/src/UserModel.hx @@ -0,0 +1,17 @@ +package ; + +import bindx.IBindable; + +@:bindable +class UserModel implements IBindable +{ + public var name:String; + public var coins:Int; + + public var health:Float = 1; + + public function new(name:String, coins:Int) { + this.name = name; + this.coins = coins; + } +} \ No newline at end of file diff --git a/samples/userInfo/src/UserView.hx b/samples/userInfo/src/UserView.hx new file mode 100644 index 0000000..0656420 --- /dev/null +++ b/samples/userInfo/src/UserView.hx @@ -0,0 +1,52 @@ +package ; + +import bindx.Bind; +import bindx.BindxExt; + +class UserView { + + @:isVar public var user(default, set):UserModel; + + var textField = new TextField(); + + var unbindOldUser:Void->Void; + + function set_user(value) { + if (user != null) { + Bind.unbind(user.health, onHealthChange); + } + if (unbindOldUser != null) { + unbindOldUser(); + unbindOldUser = null; + } + + user = value; + if (user != null) { + // BindExt.exprTo auto dispatch first time + unbindOldUser = BindExt.exprTo("Hello " + user.name + ". You have " + user.coins + " coins", this.textField.text); + + Bind.bind(user.health, onHealthChange); + // manual dispatch + onHealthChange(null, user.health); + } + return value; + } + + function onHealthChange(from:Null, to:Null) { + trace('user health changed from $from to $to'); + } + + public function new() {} +} + + +class TextField { + @:isVar public var text(default, set):String; + + function set_text(value) { + trace("TextField::text changed to: " + value); + return text = value; + } + + public function new() {} +} \ No newline at end of file