OLD | NEW |
| (Empty) |
1 library todo; | |
2 | |
3 import 'package:angular/angular.dart'; | |
4 | |
5 | |
6 class Item { | |
7 String text; | |
8 bool done; | |
9 | |
10 Item([this.text = '', this.done = false]); | |
11 | |
12 bool get isEmpty => text.isEmpty; | |
13 | |
14 Item clone() => new Item(text, done); | |
15 | |
16 void clear() { | |
17 text = ''; | |
18 done = false; | |
19 } | |
20 } | |
21 | |
22 | |
23 // ServerController interface. Logic in main.dart determines which | |
24 // implementation we should use. | |
25 abstract class ServerController { | |
26 init(TodoController todo); | |
27 } | |
28 | |
29 | |
30 // An implementation of ServerController that does nothing. | |
31 class NoServerController implements ServerController { | |
32 init(TodoController todo) { } | |
33 } | |
34 | |
35 | |
36 // An implementation of ServerController that fetches items from | |
37 // the server over HTTP. | |
38 class HttpServerController implements ServerController { | |
39 final Http _http; | |
40 HttpServerController(this._http); | |
41 | |
42 init(TodoController todo) { | |
43 _http(method: 'GET', url: '/todos').then((HttpResponse data) { | |
44 data.data.forEach((d) { | |
45 todo.items.add(new Item(d["text"], d["done"])); | |
46 }); | |
47 }); | |
48 } | |
49 } | |
50 | |
51 | |
52 @NgController( | |
53 selector: '[todo-controller]', | |
54 publishAs: 'todo') | |
55 class TodoController { | |
56 var items = <Item>[]; | |
57 Item newItem; | |
58 | |
59 TodoController(ServerController serverController) { | |
60 newItem = new Item(); | |
61 items = [ | |
62 new Item('Write Angular in Dart', true), | |
63 new Item('Write Dart in Angular'), | |
64 new Item('Do something useful') | |
65 ]; | |
66 | |
67 serverController.init(this); | |
68 } | |
69 | |
70 // workaround for https://github.com/angular/angular.dart/issues/37 | |
71 dynamic operator [](String key) => key == 'newItem' ? newItem : null; | |
72 | |
73 void add() { | |
74 if (newItem.isEmpty) return; | |
75 | |
76 items.add(newItem.clone()); | |
77 newItem.clear(); | |
78 } | |
79 | |
80 void markAllDone() { | |
81 items.forEach((item) => item.done = true); | |
82 } | |
83 | |
84 void archiveDone() { | |
85 items.removeWhere((item) => item.done); | |
86 } | |
87 | |
88 String classFor(Item item) => item.done ? 'done' : ''; | |
89 | |
90 int remaining() => items.fold(0, (count, item) => count += item.done ? 0 : 1); | |
91 } | |
OLD | NEW |