| 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 part of observe; |
| 6 * Helpers for observable objects. | |
| 7 * Intended for use with `package:observe`. | |
| 8 */ | |
| 9 library polymer.observe; | |
| 10 | |
| 11 import 'dart:async'; | |
| 12 import 'package:observe/observe.dart'; | |
| 13 | 6 |
| 14 const _VALUE = const Symbol('value'); | 7 const _VALUE = const Symbol('value'); |
| 15 | 8 |
| 16 /** | |
| 17 * Forwards an observable property from one object to another. For example: | |
| 18 * | |
| 19 * class MyModel extends ObservableBase { | |
| 20 * StreamSubscription _sub; | |
| 21 * MyOtherModel _otherModel; | |
| 22 * | |
| 23 * MyModel() { | |
| 24 * ... | |
| 25 * _sub = bindProperty(_otherModel, const Symbol('value'), | |
| 26 * () => notifyProperty(this, const Symbol('prop')); | |
| 27 * } | |
| 28 * | |
| 29 * String get prop => _otherModel.value; | |
| 30 * set prop(String value) { _otherModel.value = value; } | |
| 31 * } | |
| 32 * | |
| 33 * See also [notifyProperty]. | |
| 34 */ | |
| 35 StreamSubscription bindProperty(Observable source, Symbol sourceName, | |
| 36 void callback()) { | |
| 37 return source.changes.listen((records) { | |
| 38 for (var record in records) { | |
| 39 if (record.changes(sourceName)) { | |
| 40 callback(); | |
| 41 } | |
| 42 } | |
| 43 }); | |
| 44 } | |
| 45 | |
| 46 /** | |
| 47 * Notify the property change. Shorthand for: | |
| 48 * | |
| 49 * target.notifyChange(new PropertyChangeRecord(targetName)); | |
| 50 */ | |
| 51 void notifyProperty(Observable target, Symbol targetName) { | |
| 52 target.notifyChange(new PropertyChangeRecord(targetName)); | |
| 53 } | |
| 54 | |
| 55 | |
| 56 // Inspired by ArrayReduction at: | 9 // Inspired by ArrayReduction at: |
| 57 // https://raw.github.com/rafaelw/ChangeSummary/master/util/array_reduction.js | 10 // https://raw.github.com/rafaelw/ChangeSummary/master/util/array_reduction.js |
| 58 // The main difference is we support anything on the rich Dart Iterable API. | 11 // The main difference is we support anything on the rich Dart Iterable API. |
| 59 | 12 |
| 60 /** | 13 /** |
| 61 * Observes a path starting from each item in the list. | 14 * Observes a path starting from each item in the list. |
| 62 */ | 15 */ |
| 63 class ListPathObserver<E, P> extends ChangeNotifierBase { | 16 class ListPathObserver<E, P> extends ChangeNotifierBase { |
| 64 final ObservableList<E> list; | 17 final ObservableList<E> list; |
| 65 final String _itemPath; | 18 final String _itemPath; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 } | 68 } |
| 116 } else if (lengthAdjust < 0) { | 69 } else if (lengthAdjust < 0) { |
| 117 for (int i = 0; i < -lengthAdjust; i++) { | 70 for (int i = 0; i < -lengthAdjust; i++) { |
| 118 _subs.removeLast().cancel(); | 71 _subs.removeLast().cancel(); |
| 119 } | 72 } |
| 120 int len = _observers.length; | 73 int len = _observers.length; |
| 121 _observers.removeRange(len + lengthAdjust, len); | 74 _observers.removeRange(len + lengthAdjust, len); |
| 122 } | 75 } |
| 123 } | 76 } |
| 124 } | 77 } |
| OLD | NEW |