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