Add mountController + test

This commit is contained in:
Tobe O 2018-11-07 21:50:04 -05:00
parent 76532f9509
commit 10b6011aba
4 changed files with 21 additions and 2 deletions

View file

@ -1,6 +1,7 @@
# 2.0.0-alpha.11
* Add `readMany` to `Service`.
* Allow `ResponseContext.redirect` to take a `Uri`.
* Add `Angel.mountController`.
# 2.0.0-alpha.10
* All calls to `Service.parseId` are now affixed with the `<Id>` argument.

View file

@ -30,8 +30,10 @@ class Controller {
_app = app;
if (injectSingleton != false) {
if (!app.container.has(runtimeType)) {
_app.container.registerSingleton(this, as: runtimeType);
}
}
// Load global expose decl
var classMirror = app.container.reflector.reflectClass(this.runtimeType);

View file

@ -333,6 +333,16 @@ class Angel extends Routable {
return new Future.sync(() => configurer(this));
}
/// Shorthand for using the [container] to instantiate, and then mount a [Controller].
///
/// Just like [Container].make, in contexts without properly-reified generics (dev releases of Dart 2),
/// provide a [type] argument as well.
///
/// If you are on `Dart >=2.0.0`, simply call `mountController<T>()`..
Future mountController<T extends Controller>([Type type]) {
return configure(container.make<T>(type).configureServer);
}
/// Shorthand for calling `all('*', handler)`.
Route<RequestHandler> fallback(RequestHandler handler) {
return all('*', handler);

View file

@ -59,7 +59,13 @@ main() {
"/redirect",
(req, res) async =>
res.redirectToAction("TodoController@foo", {"foo": "world"}));
await app.configure((ctrl = new TodoController()).configureServer);
// Register as a singleton, just for the purpose of this test
if (!app.container.has<TodoController>())
app.container.registerSingleton(ctrl = new TodoController());
// Using mountController<T>();
await app.mountController<TodoController>();
print(app.controllers);
app.dumpTree();