How to implement services in RouteInitializerFn (new DSL routing)

I will switch my application to the new DSL routing. In particular, I want to do something like this with preEnter :

 final RouteInitializerFn routes =(Router router, ViewFactory views) { views.configure({ 'chat': ngRoute( path: '/chat', // authService.requireState returns a Future<bool>, and may invoke an HttpRequest preEnter: (RoutePreEnterEvent e) => e.allowEnter(authService.requireState(LOGGED_IN)), view: 'views/chat.html'), 'login': ngRoute( path: '', defaultRoute: true, view: 'views/login.html') }); } 

This will be configured in the module as follows:

value(RouteInitializerFn, routes);

If you missed it, I refer to the injectable authService in RouteInitializerFn . This is not possible because RouteInitializerFn is a function, not a class, so you cannot enter anything into it. If I encapsulated the routes function inside the class, I'm not sure how I could configure RouteInitializerFn , so I am a bit difficult.

+7
dart angular-dart
source share
1 answer

I found a pretty cool solution to this problem. It turns out that if you define a call method for a class that satisfies typedef, you can configure it as an implementation of typedef. Very cool. Here is my solution:

 class Routes { final UserService _userService; Routes(this._userService); void call(Router router, ViewFactory views) { views.configure({ 'chat': ngRoute( path: '/chat', preEnter: (RoutePreEnterEvent e) => e.allowEnter(this._userService.requireUserState(UserService.LOGGED_IN)), view: 'views/chat.html' ), 'login': ngRoute( path: '', defaultRoute: true, view: 'views/login.html' ) }); } } 

and that’s how it is configured in the module:

 // old syntax type(RouteInitializerFn, implementedBy: Routes); // new syntax bind(RouteInitializerFn, toImplementation: Routes); 
+8
source share

All Articles