| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 class ViewModel { | 7 import 'package:web_ui/observe.dart'; |
| 8 |
| 9 @observable |
| 10 class ViewModel extends Observable { |
| 8 bool isVisible(Todo todo) => todo != null && | 11 bool isVisible(Todo todo) => todo != null && |
| 9 ((showIncomplete && !todo.done) || (showDone && todo.done)); | 12 ((showIncomplete && !todo.done) || (showDone && todo.done)); |
| 10 | 13 |
| 11 bool showIncomplete = true; | 14 bool showIncomplete = true; |
| 12 | 15 |
| 13 bool showDone = true; | 16 bool showDone = true; |
| 14 } | 17 } |
| 15 | 18 |
| 16 final ViewModel viewModel = new ViewModel(); | 19 final ViewModel viewModel = new ViewModel(); |
| 17 | 20 |
| 18 // The real model: | 21 // The real model: |
| 19 | 22 |
| 20 class AppModel { | 23 @observable |
| 21 List<Todo> todos = <Todo>[]; | 24 class AppModel extends Observable { |
| 25 ObservableList<Todo> todos = new ObservableList<Todo>(); |
| 22 | 26 |
| 23 // TODO(jmesserly): remove this once List has a remove method. | 27 // TODO(jmesserly): remove this once List has a remove method. |
| 24 void removeTodo(Todo todo) { | 28 void removeTodo(Todo todo) { |
| 25 var index = todos.indexOf(todo); | 29 var index = todos.indexOf(todo); |
| 26 if (index != -1) { | 30 if (index != -1) { |
| 27 todos.removeRange(index, 1); | 31 todos.removeRange(index, 1); |
| 28 } | 32 } |
| 29 } | 33 } |
| 30 | 34 |
| 31 bool get allChecked => todos.length > 0 && todos.every((t) => t.done); | 35 bool get allChecked => todos.length > 0 && todos.every((t) => t.done); |
| 32 | 36 |
| 33 set allChecked(bool value) => todos.forEach((t) { t.done = value; }); | 37 set allChecked(bool value) => todos.forEach((t) { t.done = value; }); |
| 34 | 38 |
| 35 int get doneCount { | 39 int get doneCount { |
| 36 int res = 0; | 40 int res = 0; |
| 37 todos.forEach((t) { if (t.done) res++; }); | 41 todos.forEach((t) { if (t.done) res++; }); |
| 38 return res; | 42 return res; |
| 39 } | 43 } |
| 40 | 44 |
| 41 int get remaining => todos.length - doneCount; | 45 int get remaining => todos.length - doneCount; |
| 42 | 46 |
| 43 void clearDone() { | 47 void clearDone() { |
| 44 todos = todos.where((t) => !t.done).toList(); | 48 // TODO(jmesserly): should methods on ObservableList return Observables? |
| 49 todos = new ObservableList.from(todos.where((t) => !t.done)); |
| 45 } | 50 } |
| 46 } | 51 } |
| 47 | 52 |
| 48 final AppModel app = new AppModel(); | 53 final AppModel app = new AppModel(); |
| 49 | 54 |
| 50 class Todo { | 55 @observable |
| 56 class Todo extends Observable { |
| 51 String task; | 57 String task; |
| 52 bool done = false; | 58 bool done = false; |
| 53 | 59 |
| 54 Todo(this.task); | 60 Todo(String task) { |
| 61 // TODO(jmesserly): fix @observable so "Todo(this.task)" works. |
| 62 this.task = task; |
| 63 } |
| 55 | 64 |
| 56 String toString() => "$task ${done ? '(done)' : '(not done)'}"; | 65 String toString() => "$task ${done ? '(done)' : '(not done)'}"; |
| 57 } | 66 } |
| OLD | NEW |