-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.hx
More file actions
70 lines (56 loc) · 1.55 KB
/
Copy pathExample.hx
File metadata and controls
70 lines (56 loc) · 1.55 KB
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
package;
import tracker.Entity;
import tracker.Model;
import tracker.ds.ObservableArray;
class Example
{
static function main()
{
new Context();
}
}
class Context extends Entity
{
public function new()
{
super();
var testPerson = new Person("Dave", 25);
testPerson.onNameChange(this, (current, previous) -> {
trace("Name changed from " + previous + " to " + current);
});
testPerson.onAgeChange(this, (current, previous) -> {
trace("Age changed from " + previous + " to " + current);
});
testPerson.name = "John";
testPerson.age = 30;
testPerson.nicknames.onChange(this, () -> {
trace("Nicknames changed!");
});
testPerson.nicknames.onItemInserted(this, (index, item) -> {
trace("Nickname added at " + index + ": " + item);
});
testPerson.nicknames.onItemRemoved(this, (index, item) -> {
trace("Nickname removed at " + index + ": " + item);
});
testPerson.nicknames.push("Johnny");
testPerson.nicknames.push("JP");
testPerson.nicknames.push("Davy");
testPerson.nicknames.remove("JP");
trace('nicknames.length = ${testPerson.nicknames.length}'); // 2
trace('nicknames[1] = ${testPerson.nicknames[1]}'); // Davy
}
}
class Person extends Model
{
@observe public var name:String;
@observe public var age:Int;
@observe public var nicknames:ObservableArray<String>;
public function new(name:String, age:Int)
{
super();
this.name = name;
this.age = age;
this.nicknames = new ObservableArray();
autorun(() -> trace('Person: ${this.name}, Age: ${this.age}, Nicknames: ${this.nicknames.toArray()}'));
}
}