first sample
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
-main Main
|
||||||
|
-cp src
|
||||||
|
#-lib bindx2
|
||||||
|
-cp ../../src
|
||||||
|
--interp
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<Float>, to:Null<Float>) {
|
||||||
|
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() {}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user