Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: example/todomvc/model.dart

Issue 12225039: Support for observable models, fixes #259 (Closed) Base URL: https://github.com/dart-lang/web-ui.git@master
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « example/todomvc/main.dart ('k') | example/todomvc/router_options.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import 'package:web_ui/observe.dart';
8 import 'package:web_ui/observe/html.dart';
9
10 @observable
7 class ViewModel { 11 class ViewModel {
8 bool isVisible(Todo todo) => todo != null && 12 bool isVisible(Todo todo) => todo != null &&
9 ((showIncomplete && !todo.done) || (showDone && todo.done)); 13 ((showIncomplete && !todo.done) || (showDone && todo.done));
10 14
11 bool showIncomplete = true; 15 bool get showIncomplete => locationHash != '#/completed';
12 16
13 bool showDone = true; 17 bool get showDone => locationHash != '#/active';
14 } 18 }
15 19
16 final ViewModel viewModel = new ViewModel(); 20 final ViewModel viewModel = new ViewModel();
17 21
18 // The real model: 22 // The real model:
19 23
24 @observable
20 class AppModel { 25 class AppModel {
21 List<Todo> todos = <Todo>[]; 26 ObservableList<Todo> todos = new ObservableList<Todo>();
22 27
23 // TODO(jmesserly): remove this once List has a remove method. 28 // TODO(jmesserly): remove this once List has a remove method.
24 void removeTodo(Todo todo) { 29 void removeTodo(Todo todo) {
25 var index = todos.indexOf(todo); 30 var index = todos.indexOf(todo);
26 if (index != -1) { 31 if (index != -1) {
27 todos.removeRange(index, 1); 32 todos.removeRange(index, 1);
28 } 33 }
29 } 34 }
30 35
31 bool get allChecked => todos.length > 0 && todos.every((t) => t.done); 36 bool get allChecked => todos.length > 0 && todos.every((t) => t.done);
32 37
33 set allChecked(bool value) => todos.forEach((t) { t.done = value; }); 38 set allChecked(bool value) => todos.forEach((t) { t.done = value; });
34 39
35 int get doneCount { 40 int get doneCount {
36 int res = 0; 41 int res = 0;
37 todos.forEach((t) { if (t.done) res++; }); 42 todos.forEach((t) { if (t.done) res++; });
38 return res; 43 return res;
39 } 44 }
40 45
41 int get remaining => todos.length - doneCount; 46 int get remaining => todos.length - doneCount;
42 47
43 void clearDone() { 48 void clearDone() {
44 todos = todos.where((t) => !t.done).toList(); 49 // TODO(jmesserly): should methods on ObservableList return Observables?
50 todos = toObservable(todos.where((t) => !t.done));
45 } 51 }
46 } 52 }
47 53
48 final AppModel app = new AppModel(); 54 final AppModel app = new AppModel();
49 55
56 @observable
50 class Todo { 57 class Todo {
51 String task; 58 String task;
52 bool done = false; 59 bool done = false;
53 60
54 Todo(this.task); 61 Todo(this.task);
55 62
56 String toString() => "$task ${done ? '(done)' : '(not done)'}"; 63 String toString() => "$task ${done ? '(done)' : '(not done)'}";
57 } 64 }
OLDNEW
« no previous file with comments | « example/todomvc/main.dart ('k') | example/todomvc/router_options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698