Created
May 29, 2014 00:46
-
-
Save watilde/1bb060a9d8854c34e324 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library todo; | |
import 'dart:html'; | |
import 'package:angular/angular.dart'; | |
import 'package:angular/application_factory.dart'; | |
import 'package:angular/playback/playback_http.dart'; | |
class Item { | |
String text; | |
bool done; | |
Item([this.text = '', this.done = false]); | |
bool get isEmpty => text.isEmpty; | |
Item clone() => new Item(text, done); | |
void clear() { | |
text = ''; | |
done = false; | |
} | |
} | |
// ServerController interface. Logic in main.dart determines which | |
// implementation we should use. | |
abstract class Server { | |
init(Todo todo); | |
} | |
// An implementation of ServerController that does nothing. | |
@Injectable() | |
class NoOpServer implements Server { | |
init(Todo todo) { } | |
} | |
// An implementation of ServerController that fetches items from | |
// the server over HTTP. | |
@Injectable() | |
class HttpServer implements Server { | |
final Http _http; | |
HttpServer(this._http); | |
init(Todo todo) { | |
_http(method: 'GET', url: '/todos').then((HttpResponse data) { | |
data.data.forEach((d) { | |
todo.items.add(new Item(d["text"], d["done"])); | |
}); | |
}); | |
} | |
} | |
@Controller( | |
selector: '[todo-controller]', | |
publishAs: 'todo') | |
class Todo { | |
var items = <Item>[]; | |
Item newItem; | |
Todo(Server serverController) { | |
newItem = new Item(); | |
items = [ | |
new Item('Write Angular in Dart', true), | |
new Item('Write Dart in Angular'), | |
new Item('Do something useful') | |
]; | |
serverController.init(this); | |
} | |
void add() { | |
if (newItem.isEmpty) return; | |
items.add(newItem.clone()); | |
newItem.clear(); | |
} | |
void markAllDone() { | |
items.forEach((item) => item.done = true); | |
} | |
void archiveDone() { | |
items.removeWhere((item) => item.done); | |
} | |
String classFor(Item item) => item.done ? 'done' : ''; | |
int remaining() => items.fold(0, (count, item) => count += item.done ? 0 : 1); | |
} | |
main() { | |
print(window.location.search); | |
var module = new Module() | |
..bind(Todo) | |
..bind(PlaybackHttpBackendConfig); | |
// If these is a query in the URL, use the server-backed | |
// controller. Otherwise, use the stored-data controller. | |
var query = window.location.search; | |
if (query.contains('?')) { | |
module.bind(Server, toImplementation: HttpServer); | |
} else { | |
module.bind(Server, toImplementation: NoOpServer); | |
} | |
if (query == '?record') { | |
print('Using recording HttpBackend'); | |
var wrapper = new HttpBackendWrapper(new HttpBackend()); | |
module.bind(HttpBackendWrapper, toValue: new HttpBackendWrapper(new HttpBackend())); | |
module.bind(HttpBackend, toImplementation: RecordingHttpBackend); | |
} | |
if (query == '?playback') { | |
print('Using playback HttpBackend'); | |
module.bind(HttpBackend, toImplementation: PlaybackHttpBackend); | |
} | |
applicationFactory().addModule(module).run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment