| 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 library model; | 5 library model; |
| 6 | 6 |
| 7 import 'dart:html' show CompoundBinding; | 7 import 'dart:html' show CompoundBinding; |
| 8 import 'package:polymer/polymer.dart'; | 8 import 'package:polymer/polymer.dart'; |
| 9 | 9 |
| 10 final appModel = new AppModel._(); | 10 final appModel = new AppModel._(); |
| 11 | 11 |
| 12 class AppModel extends ObservableBase { | 12 class AppModel extends ObservableBase { |
| 13 final ObservableList<Todo> todos = new ObservableList<Todo>(); | 13 final ObservableList<Todo> todos = new ObservableList<Todo>(); |
| 14 @observable int doneCount; | 14 @observable int doneCount; |
| 15 @observable int remaining; | 15 @observable int remaining; |
| 16 @observable List<Todo> visibleTodos; | 16 @observable List<Todo> visibleTodos; |
| 17 @observable bool hasTodos; | |
| 18 @observable bool hasCompleteTodos; | 17 @observable bool hasCompleteTodos; |
| 19 | 18 |
| 20 bool _allChecked; | 19 bool _allChecked; |
| 21 | 20 |
| 22 AppModel._() { | 21 AppModel._() { |
| 23 // TODO(jmesserly): need to make this easier. | |
| 24 new ListPathObserver(todos, 'done').changes.listen(_updateTodoDone); | 22 new ListPathObserver(todos, 'done').changes.listen(_updateTodoDone); |
| 25 windowLocation.changes.listen(_updateVisibleTodos); | 23 windowLocation.changes.listen(_updateVisibleTodos); |
| 26 _updateTodoDone(null); | 24 _updateTodoDone(null); |
| 27 } | 25 } |
| 28 | 26 |
| 29 _updateTodoDone(_) { | 27 _updateTodoDone(_) { |
| 28 // TODO(jmesserly): we should try using fancy-syntax expressions and filters |
| 29 // instead of computing so many things. |
| 30 doneCount = todos.fold(0, (count, t) => count + (t.done ? 1 : 0)); | 30 doneCount = todos.fold(0, (count, t) => count + (t.done ? 1 : 0)); |
| 31 hasCompleteTodos = doneCount > 0; | 31 hasCompleteTodos = doneCount > 0; |
| 32 remaining = todos.length - doneCount; | 32 remaining = todos.length - doneCount; |
| 33 hasTodos = todos.length > 0; | |
| 34 | 33 |
| 35 _allChecked = notifyPropertyChange(const Symbol('allChecked'), | 34 _allChecked = notifyPropertyChange(const Symbol('allChecked'), |
| 36 _allChecked, hasTodos && remaining == 0); | 35 _allChecked, todos.length > 0 && remaining == 0); |
| 37 | 36 |
| 38 _updateVisibleTodos(_); | 37 _updateVisibleTodos(_); |
| 39 } | 38 } |
| 40 | 39 |
| 41 _updateVisibleTodos(_) { | 40 _updateVisibleTodos(_) { |
| 42 bool filterDone = null; | 41 bool filterDone = null; |
| 43 if (windowLocation.hash == '#/completed') { | 42 if (windowLocation.hash == '#/completed') { |
| 44 filterDone = true; | 43 filterDone = true; |
| 45 } else if (windowLocation.hash == '#/active') { | 44 } else if (windowLocation.hash == '#/active') { |
| 46 filterDone = false; | 45 filterDone = false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 61 } | 60 } |
| 62 | 61 |
| 63 class Todo extends ObservableBase { | 62 class Todo extends ObservableBase { |
| 64 @observable String task; | 63 @observable String task; |
| 65 @observable bool done = false; | 64 @observable bool done = false; |
| 66 | 65 |
| 67 Todo(this.task); | 66 Todo(this.task); |
| 68 | 67 |
| 69 String toString() => "$task ${done ? '(done)' : '(not done)'}"; | 68 String toString() => "$task ${done ? '(done)' : '(not done)'}"; |
| 70 } | 69 } |
| OLD | NEW |