Skip to content

Commit daef71b

Browse files
committed
refactor(routing): make more field private & final, add return types
1 parent 165e4ad commit daef71b

File tree

4 files changed

+51
-49
lines changed

4 files changed

+51
-49
lines changed

lib/routing/module.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ class RoutingModule extends Module {
141141
bind(NgRoutingUsePushState);
142142
bind(Router, toFactory: (injector) {
143143
var useFragment = !injector.get(NgRoutingUsePushState).usePushState;
144-
return new Router(useFragment: useFragment,
145-
windowImpl: injector.get(Window));
144+
return new Router(useFragment: useFragment, windowImpl: injector.get(Window));
146145
});
147146
bind(NgRoutingHelper);
148147
bind(RouteProvider, toValue: null);

lib/routing/ng_bind_route.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ part of angular.routing;
2727
module: NgBindRoute.module,
2828
map: const {'ng-bind-route': '@routeName'})
2929
class NgBindRoute implements RouteProvider {
30-
Router _router;
3130
String routeName;
32-
Injector _injector;
31+
final Router _router;
32+
final Injector _injector;
3333

3434
static final Module _module = new Module()
3535
..bind(RouteProvider, toFactory: (i) => i.get(NgBindRoute),
3636
visibility: Directive.CHILDREN_VISIBILITY);
37-
static module() => _module;
37+
38+
static Module module() => _module;
3839

3940
// We inject NgRoutingHelper to force initialization of routing.
4041
NgBindRoute(this._router, this._injector, NgRoutingHelper _);

lib/routing/ng_view.dart

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,48 +53,46 @@ part of angular.routing;
5353
*/
5454
@Decorator(
5555
selector: 'ng-view',
56-
module: NgView.module,
57-
visibility: Directive.CHILDREN_VISIBILITY)
56+
module: NgView.module)
5857
class NgView implements DetachAware, RouteProvider {
5958
static final Module _module = new Module()
6059
..bind(RouteProvider, toFactory: (i) => i.get(NgView));
61-
static module() => _module;
6260

63-
final NgRoutingHelper locationService;
64-
final ViewCache viewCache;
65-
final Injector injector;
66-
final Element element;
67-
final Scope scope;
61+
static Module module() => _module;
62+
63+
final NgRoutingHelper _locationService;
64+
final ViewCache _viewCache;
65+
final Injector _injector;
66+
final Element _element;
67+
final Scope _scope;
6868
RouteHandle _route;
6969

7070
View _view;
71-
Scope _scope;
71+
Scope _childScope;
7272
Route _viewRoute;
7373

74-
NgView(this.element, this.viewCache,
75-
Injector injector, Router router,
76-
this.scope)
77-
: injector = injector,
78-
locationService = injector.get(NgRoutingHelper)
74+
NgView(this._element, this._viewCache, Injector injector, Router router, this._scope)
75+
: _injector = injector,
76+
_locationService = injector.get(NgRoutingHelper)
7977
{
8078
RouteProvider routeProvider = injector.parent.get(NgView);
8179
_route = routeProvider != null ?
8280
routeProvider.route.newHandle() :
8381
router.root.newHandle();
84-
locationService._registerPortal(this);
82+
_locationService._registerPortal(this);
8583
_maybeReloadViews();
8684
}
8785

8886
void _maybeReloadViews() {
89-
if (_route.isActive) locationService._reloadViews(startingFrom: _route);
87+
if (_route.isActive) _locationService._reloadViews(startingFrom: _route);
9088
}
9189

92-
detach() {
90+
void detach() {
9391
_route.discard();
94-
locationService._unregisterPortal(this);
92+
_locationService._unregisterPortal(this);
9593
}
9694

97-
_show(_View viewDef, Route route, List<Module> modules) {
95+
void _show(_View viewDef, Route route, List<Module> modules) {
9896
assert(route.isActive);
9997

10098
if (_viewRoute != null) return;
@@ -109,34 +107,36 @@ class NgView implements DetachAware, RouteProvider {
109107
});
110108

111109
var viewInjector = modules == null ?
112-
injector :
113-
forceNewDirectivesAndFormatters(injector, modules);
110+
_injector :
111+
forceNewDirectivesAndFormatters(_injector, modules);
114112

115113
var newDirectives = viewInjector.get(DirectiveMap);
116114
var viewFuture = viewDef.templateHtml != null ?
117-
new Future.value(viewCache.fromHtml(viewDef.templateHtml, newDirectives)) :
118-
viewCache.fromUrl(viewDef.template, newDirectives);
115+
new Future.value(_viewCache.fromHtml(viewDef.templateHtml, newDirectives)) :
116+
_viewCache.fromUrl(viewDef.template, newDirectives);
119117
viewFuture.then((viewFactory) {
120118
_cleanUp();
121-
_scope = scope.createChild(new PrototypeMap(scope.context));
119+
_childScope = _scope.createChild(new PrototypeMap(_scope.context));
122120
_view = viewFactory(
123-
viewInjector.createChild([new Module()..bind(Scope, toValue: _scope)]));
124-
_view.nodes.forEach((elm) => element.append(elm));
121+
viewInjector.createChild([new Module()..bind(Scope, toValue: _childScope)]));
122+
_view.nodes.forEach((elm) => _element.append(elm));
125123
});
126124
}
127125

128-
_cleanUp() {
126+
void _cleanUp() {
129127
if (_view == null) return;
130128

131129
_view.nodes.forEach((node) => node.remove());
132-
_scope.destroy();
130+
_childScope.destroy();
133131

134132
_view = null;
135-
_scope = null;
133+
_childScope = null;
136134
}
137135

138136
Route get route => _viewRoute;
137+
139138
String get routeName => _viewRoute.name;
139+
140140
Map<String, String> get parameters {
141141
var res = <String, String>{};
142142
var p = _viewRoute;

lib/routing/routing.dart

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ class RouteViewFactory {
88

99
RouteViewFactory(this.locationService);
1010

11-
call(String templateUrl) =>
11+
Function call(String templateUrl) =>
1212
(RouteEnterEvent event) => _enterHandler(event, templateUrl);
1313

14-
_enterHandler(RouteEnterEvent event, String templateUrl,
15-
{List<Module> modules, String templateHtml}) =>
16-
locationService._route(event.route, templateUrl, fromEvent: true,
17-
modules: modules, templateHtml: templateHtml);
14+
void _enterHandler(RouteEnterEvent event, String templateUrl,
15+
{List<Module> modules, String templateHtml}) {
16+
locationService._route(event.route, templateUrl, fromEvent: true,
17+
modules: modules, templateHtml: templateHtml);
18+
}
1819

19-
configure(Map<String, NgRouteCfg> config) =>
20-
_configure(locationService.router.root, config);
20+
void configure(Map<String, NgRouteCfg> config) {
21+
_configure(locationService.router.root, config);
22+
}
2123

22-
_configure(Route route, Map<String, NgRouteCfg> config) {
24+
void _configure(Route route, Map<String, NgRouteCfg> config) {
2325
config.forEach((name, cfg) {
2426
var modulesCalled = false;
2527
List<Module> newModules;
@@ -115,8 +117,8 @@ typedef void RouteInitializerFn(Router router, RouteViewFactory viewFactory);
115117
class NgRoutingHelper {
116118
final Router router;
117119
final Application _ngApp;
118-
List<NgView> portals = <NgView>[];
119-
Map<String, _View> _templates = new Map<String, _View>();
120+
final _portals = <NgView>[];
121+
final _templates = <String, _View>{};
120122

121123
NgRoutingHelper(RouteInitializer initializer, Injector injector, this.router,
122124
this._ngApp) {
@@ -136,7 +138,7 @@ class NgRoutingHelper {
136138
router.onRouteStart.listen((RouteStartEvent routeEvent) {
137139
routeEvent.completed.then((success) {
138140
if (success) {
139-
portals.forEach((NgView p) => p._maybeReloadViews());
141+
_portals.forEach((NgView p) => p._maybeReloadViews());
140142
}
141143
});
142144
});
@@ -155,7 +157,7 @@ class NgRoutingHelper {
155157
if (viewDef == null) continue;
156158
var templateUrl = viewDef.template;
157159

158-
NgView view = portals.lastWhere((NgView v) {
160+
NgView view = _portals.lastWhere((NgView v) {
159161
return _routePath(route) != _routePath(v._route) &&
160162
_routePath(route).startsWith(_routePath(v._route));
161163
}, orElse: () => null);
@@ -173,11 +175,11 @@ class NgRoutingHelper {
173175
}
174176

175177
void _registerPortal(NgView ngView) {
176-
portals.add(ngView);
178+
_portals.add(ngView);
177179
}
178180

179181
void _unregisterPortal(NgView ngView) {
180-
portals.remove(ngView);
182+
_portals.remove(ngView);
181183
}
182184
}
183185

@@ -190,7 +192,7 @@ class _View {
190192
}
191193

192194
String _routePath(Route route) {
193-
var path = [];
195+
final path = [];
194196
var p = route;
195197
while (p.parent != null) {
196198
path.insert(0, p.name);

0 commit comments

Comments
 (0)