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