| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library todomvc.web.model; | |
| 6 | |
| 7 import 'package:polymer/polymer.dart'; | |
| 8 | |
| 9 final appModel = new AppModel._(); | |
| 10 | |
| 11 @reflectable | |
| 12 class AppModel extends Observable { | |
| 13 final ObservableList<Todo> todos = new ObservableList<Todo>(); | |
| 14 @observable int doneCount; | |
| 15 @observable int remaining; | |
| 16 @observable List<Todo> visibleTodos; | |
| 17 @observable bool hasCompleteTodos; | |
| 18 | |
| 19 bool _allChecked; | |
| 20 | |
| 21 AppModel._() { | |
| 22 new ListPathObserver(todos, 'done').changes.listen(_updateTodoDone); | |
| 23 windowLocation.changes.listen(_updateVisibleTodos); | |
| 24 _updateTodoDone(null); | |
| 25 } | |
| 26 | |
| 27 _updateTodoDone(_) { | |
| 28 // TODO(jmesserly): we should try using Polymer Expressions and filters | |
| 29 // instead of computing so many things. | |
| 30 doneCount = todos.fold(0, (count, t) => count + (t.done ? 1 : 0)); | |
| 31 hasCompleteTodos = doneCount > 0; | |
| 32 remaining = todos.length - doneCount; | |
| 33 | |
| 34 _allChecked = notifyPropertyChange(const Symbol('allChecked'), | |
| 35 _allChecked, todos.length > 0 && remaining == 0); | |
| 36 | |
| 37 _updateVisibleTodos(_); | |
| 38 } | |
| 39 | |
| 40 _updateVisibleTodos(_) { | |
| 41 bool filterDone = null; | |
| 42 if (windowLocation.hash == '#/completed') { | |
| 43 filterDone = true; | |
| 44 } else if (windowLocation.hash == '#/active') { | |
| 45 filterDone = false; | |
| 46 } | |
| 47 | |
| 48 visibleTodos = todos.where( | |
| 49 (t) => filterDone == null || t.done == filterDone) | |
| 50 .toList(growable: false); | |
| 51 } | |
| 52 | |
| 53 // TODO(jmesserly): the @observable here is temporary. | |
| 54 bool get allChecked => _allChecked; | |
| 55 set allChecked(bool value) { | |
| 56 todos.forEach((t) { t.done = value; }); | |
| 57 } | |
| 58 | |
| 59 void clearDone() => todos.removeWhere((t) => t.done); | |
| 60 } | |
| 61 | |
| 62 class Todo extends Observable { | |
| 63 @observable String task; | |
| 64 @observable bool done = false; | |
| 65 | |
| 66 Todo(this.task); | |
| 67 | |
| 68 String toString() => "$task ${done ? '(done)' : '(not done)'}"; | |
| 69 } | |
| OLD | NEW |