| OLD | NEW |
| (Empty) |
| 1 library todo; | |
| 2 | |
| 3 import 'dart:html'; | |
| 4 | |
| 5 import 'package:angular/angular.dart'; | |
| 6 import 'package:angular/application_factory.dart'; | |
| 7 import 'package:angular/playback/playback_http.dart'; | |
| 8 | |
| 9 class Item { | |
| 10 String text; | |
| 11 bool done; | |
| 12 | |
| 13 Item([this.text = '', this.done = false]); | |
| 14 | |
| 15 bool get isEmpty => text.isEmpty; | |
| 16 | |
| 17 Item clone() => new Item(text, done); | |
| 18 | |
| 19 void clear() { | |
| 20 text = ''; | |
| 21 done = false; | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 | |
| 26 // ServerController interface. Logic in main.dart determines which | |
| 27 // implementation we should use. | |
| 28 abstract class Server { | |
| 29 init(Todo todo); | |
| 30 } | |
| 31 | |
| 32 | |
| 33 // An implementation of ServerController that does nothing. | |
| 34 @Injectable() | |
| 35 class NoOpServer implements Server { | |
| 36 init(Todo todo) { } | |
| 37 } | |
| 38 | |
| 39 | |
| 40 // An implementation of ServerController that fetches items from | |
| 41 // the server over HTTP. | |
| 42 @Injectable() | |
| 43 class HttpServer implements Server { | |
| 44 final Http _http; | |
| 45 HttpServer(this._http); | |
| 46 | |
| 47 init(Todo todo) { | |
| 48 _http(method: 'GET', url: '/todos').then((HttpResponse data) { | |
| 49 data.data.forEach((d) { | |
| 50 todo.items.add(new Item(d["text"], d["done"])); | |
| 51 }); | |
| 52 }); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 | |
| 57 @Controller( | |
| 58 selector: '[todo-controller]', | |
| 59 publishAs: 'todo') | |
| 60 class Todo { | |
| 61 var items = <Item>[]; | |
| 62 Item newItem; | |
| 63 | |
| 64 Todo(Server serverController) { | |
| 65 newItem = new Item(); | |
| 66 items = [ | |
| 67 new Item('Write Angular in Dart', true), | |
| 68 new Item('Write Dart in Angular'), | |
| 69 new Item('Do something useful') | |
| 70 ]; | |
| 71 | |
| 72 serverController.init(this); | |
| 73 } | |
| 74 | |
| 75 void add() { | |
| 76 if (newItem.isEmpty) return; | |
| 77 | |
| 78 items.add(newItem.clone()); | |
| 79 newItem.clear(); | |
| 80 } | |
| 81 | |
| 82 void markAllDone() { | |
| 83 items.forEach((item) => item.done = true); | |
| 84 } | |
| 85 | |
| 86 void archiveDone() { | |
| 87 items.removeWhere((item) => item.done); | |
| 88 } | |
| 89 | |
| 90 String classFor(Item item) => item.done ? 'done' : ''; | |
| 91 | |
| 92 int remaining() => items.fold(0, (count, item) => count += item.done ? 0 : 1); | |
| 93 } | |
| 94 | |
| 95 main() { | |
| 96 print(window.location.search); | |
| 97 var module = new Module() | |
| 98 ..type(Todo) | |
| 99 ..type(PlaybackHttpBackendConfig); | |
| 100 | |
| 101 // If these is a query in the URL, use the server-backed | |
| 102 // TodoController. Otherwise, use the stored-data controller. | |
| 103 var query = window.location.search; | |
| 104 if (query.contains('?')) { | |
| 105 module.type(Server, implementedBy: HttpServer); | |
| 106 } else { | |
| 107 module.type(Server, implementedBy: NoOpServer); | |
| 108 } | |
| 109 | |
| 110 if (query == '?record') { | |
| 111 print('Using recording HttpBackend'); | |
| 112 var wrapper = new HttpBackendWrapper(new HttpBackend()); | |
| 113 module.value(HttpBackendWrapper, new HttpBackendWrapper(new HttpBackend())); | |
| 114 module.type(HttpBackend, implementedBy: RecordingHttpBackend); | |
| 115 } | |
| 116 | |
| 117 if (query == '?playback') { | |
| 118 print('Using playback HttpBackend'); | |
| 119 module.type(HttpBackend, implementedBy: PlaybackHttpBackend); | |
| 120 } | |
| 121 | |
| 122 applicationFactory().addModule(module).run(); | |
| 123 } | |
| OLD | NEW |