OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Support for observing changes in model-view architectures. | 6 * Support for observing changes in model-view architectures. |
7 * | 7 * |
8 * **Warning:** This library is experimental, and APIs are subject to change. | 8 * **Warning:** This library is experimental, and APIs are subject to change. |
9 * | 9 * |
10 * This library is used to observe changes to [Observable] types. It also | 10 * This library is used to observe changes to [Observable] types. It also |
(...skipping 25 matching lines...) Expand all Loading... | |
36 * Observable.dirtyCheck(); | 36 * Observable.dirtyCheck(); |
37 * print('done!'); | 37 * print('done!'); |
38 * } | 38 * } |
39 * | 39 * |
40 * A more sophisticated approach is to implement the change notification | 40 * A more sophisticated approach is to implement the change notification |
41 * manually. This avoids the potentially expensive [Observable.dirtyCheck] | 41 * manually. This avoids the potentially expensive [Observable.dirtyCheck] |
42 * operation, but requires more work in the object: | 42 * operation, but requires more work in the object: |
43 * | 43 * |
44 * class Monster extends Unit with ChangeNotifierMixin { | 44 * class Monster extends Unit with ChangeNotifierMixin { |
45 * int _health = 100; | 45 * int _health = 100; |
46 * get health => _health; | 46 * @reflectable get health => _health; |
47 * set health(val) { | 47 * @reflectable set health(val) { |
48 * _health = notifyPropertyChange(#health, _health, val); | 48 * _health = notifyPropertyChange(#health, _health, val); |
49 * } | 49 * } |
50 * | 50 * |
51 * void damage(int amount) { | 51 * void damage(int amount) { |
52 * print('$this takes $amount damage!'); | 52 * print('$this takes $amount damage!'); |
53 * health -= amount; | 53 * health -= amount; |
54 * } | 54 * } |
55 * | 55 * |
56 * toString() => 'Monster with $health hit points'; | 56 * toString() => 'Monster with $health hit points'; |
57 * } | 57 * } |
58 * | 58 * |
59 * main() { | 59 * main() { |
60 * var obj = new Monster(); | 60 * var obj = new Monster(); |
61 * obj.changes.listen((records) { | 61 * obj.changes.listen((records) { |
62 * print('Changes to $obj were: $records'); | 62 * print('Changes to $obj were: $records'); |
63 * }); | 63 * }); |
64 * // Schedules asynchronous delivery of these changes | 64 * // Schedules asynchronous delivery of these changes |
65 * obj.damage(10); | 65 * obj.damage(10); |
66 * obj.damage(20); | 66 * obj.damage(20); |
67 * print('done!'); | 67 * print('done!'); |
68 * } | 68 * } |
69 * | 69 * |
70 * *Note*: it is good practice to keep `@reflectable` annotation on | |
71 * getters/setters so they are accessible via reflection. This will preserve | |
72 * them from tree-shaking. You can also put this annotation on the class and it | |
73 * preserve all of its members for reflection. | |
74 * | |
70 * [Tools](https://www.dartlang.org/polymer-dart/) exist to convert the first | 75 * [Tools](https://www.dartlang.org/polymer-dart/) exist to convert the first |
71 * form into the second form automatically, to get the best of both worlds. | 76 * form into the second form automatically, to get the best of both worlds. |
72 */ | 77 */ |
73 library observe; | 78 library observe; |
74 | 79 |
75 import 'dart:async'; | 80 import 'dart:async'; |
76 import 'dart:collection'; | 81 import 'dart:collection'; |
82 | |
83 // Note: ObservableProperty is in this list only for the unusual use case of | |
84 // dart2js without deploy tool. The deploy tool (see "transformer.dart") will | |
85 // add the @reflectable annotation, which makes it work with Polymer's | |
86 // @published. | |
87 @MirrorsUsed(metaTargets: const [Reflectable, ObservableProperty], | |
88 override: 'observe') | |
Siggi Cherem (dart-lang)
2013/10/15 21:52:18
do you need the override here? (I thought without
Jennifer Messerly
2013/10/15 22:03:29
if overrides are null the annotation is ignored:
h
Siggi Cherem (dart-lang)
2013/10/15 22:06:45
:-( --- then either the code or the documentation
| |
77 import 'dart:mirrors'; | 89 import 'dart:mirrors'; |
78 | 90 |
79 // Note: this is an internal library so we can import it from tests. | 91 // Note: this is an internal library so we can import it from tests. |
80 // TODO(jmesserly): ideally we could import this with a prefix, but it caused | 92 // TODO(jmesserly): ideally we could import this with a prefix, but it caused |
81 // strange problems on the VM when I tested out the dirty-checking example | 93 // strange problems on the VM when I tested out the dirty-checking example |
82 // above. | 94 // above. |
83 import 'src/dirty_check.dart'; | 95 import 'src/dirty_check.dart'; |
84 | 96 |
85 part 'src/bind_property.dart'; | 97 part 'src/bind_property.dart'; |
86 part 'src/change_notifier.dart'; | 98 part 'src/change_notifier.dart'; |
87 part 'src/change_record.dart'; | 99 part 'src/change_record.dart'; |
88 part 'src/compound_binding.dart'; | 100 part 'src/compound_binding.dart'; |
89 part 'src/list_path_observer.dart'; | 101 part 'src/list_path_observer.dart'; |
102 part 'src/metadata.dart'; | |
90 part 'src/observable.dart'; | 103 part 'src/observable.dart'; |
91 part 'src/observable_box.dart'; | 104 part 'src/observable_box.dart'; |
92 part 'src/observable_list.dart'; | 105 part 'src/observable_list.dart'; |
93 part 'src/observable_map.dart'; | 106 part 'src/observable_map.dart'; |
94 part 'src/path_observer.dart'; | 107 part 'src/path_observer.dart'; |
95 part 'src/to_observable.dart'; | 108 part 'src/to_observable.dart'; |
OLD | NEW |