Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
refactor(routing): make more field private & final, add return types
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed May 27, 2014
1 parent 165e4ad commit daef71b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 49 deletions.
3 changes: 1 addition & 2 deletions lib/routing/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ class RoutingModule extends Module {
bind(NgRoutingUsePushState);
bind(Router, toFactory: (injector) {
var useFragment = !injector.get(NgRoutingUsePushState).usePushState;
return new Router(useFragment: useFragment,
windowImpl: injector.get(Window));
return new Router(useFragment: useFragment, windowImpl: injector.get(Window));
});
bind(NgRoutingHelper);
bind(RouteProvider, toValue: null);
Expand Down
7 changes: 4 additions & 3 deletions lib/routing/ng_bind_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ part of angular.routing;
module: NgBindRoute.module,
map: const {'ng-bind-route': '@routeName'})
class NgBindRoute implements RouteProvider {
Router _router;
String routeName;
Injector _injector;
final Router _router;
final Injector _injector;

static final Module _module = new Module()
..bind(RouteProvider, toFactory: (i) => i.get(NgBindRoute),
visibility: Directive.CHILDREN_VISIBILITY);
static module() => _module;

static Module module() => _module;

// We inject NgRoutingHelper to force initialization of routing.
NgBindRoute(this._router, this._injector, NgRoutingHelper _);
Expand Down
58 changes: 29 additions & 29 deletions lib/routing/ng_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,48 +53,46 @@ part of angular.routing;
*/
@Decorator(
selector: 'ng-view',
module: NgView.module,
visibility: Directive.CHILDREN_VISIBILITY)
module: NgView.module)
class NgView implements DetachAware, RouteProvider {
static final Module _module = new Module()
..bind(RouteProvider, toFactory: (i) => i.get(NgView));
static module() => _module;

final NgRoutingHelper locationService;
final ViewCache viewCache;
final Injector injector;
final Element element;
final Scope scope;
static Module module() => _module;

final NgRoutingHelper _locationService;
final ViewCache _viewCache;
final Injector _injector;
final Element _element;
final Scope _scope;
RouteHandle _route;

View _view;
Scope _scope;
Scope _childScope;
Route _viewRoute;

NgView(this.element, this.viewCache,
Injector injector, Router router,
this.scope)
: injector = injector,
locationService = injector.get(NgRoutingHelper)
NgView(this._element, this._viewCache, Injector injector, Router router, this._scope)
: _injector = injector,
_locationService = injector.get(NgRoutingHelper)
{
RouteProvider routeProvider = injector.parent.get(NgView);
_route = routeProvider != null ?
routeProvider.route.newHandle() :
router.root.newHandle();
locationService._registerPortal(this);
_locationService._registerPortal(this);
_maybeReloadViews();
}

void _maybeReloadViews() {
if (_route.isActive) locationService._reloadViews(startingFrom: _route);
if (_route.isActive) _locationService._reloadViews(startingFrom: _route);
}

detach() {
void detach() {
_route.discard();
locationService._unregisterPortal(this);
_locationService._unregisterPortal(this);
}

_show(_View viewDef, Route route, List<Module> modules) {
void _show(_View viewDef, Route route, List<Module> modules) {
assert(route.isActive);

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

var viewInjector = modules == null ?
injector :
forceNewDirectivesAndFormatters(injector, modules);
_injector :
forceNewDirectivesAndFormatters(_injector, modules);

var newDirectives = viewInjector.get(DirectiveMap);
var viewFuture = viewDef.templateHtml != null ?
new Future.value(viewCache.fromHtml(viewDef.templateHtml, newDirectives)) :
viewCache.fromUrl(viewDef.template, newDirectives);
new Future.value(_viewCache.fromHtml(viewDef.templateHtml, newDirectives)) :
_viewCache.fromUrl(viewDef.template, newDirectives);
viewFuture.then((viewFactory) {
_cleanUp();
_scope = scope.createChild(new PrototypeMap(scope.context));
_childScope = _scope.createChild(new PrototypeMap(_scope.context));
_view = viewFactory(
viewInjector.createChild([new Module()..bind(Scope, toValue: _scope)]));
_view.nodes.forEach((elm) => element.append(elm));
viewInjector.createChild([new Module()..bind(Scope, toValue: _childScope)]));
_view.nodes.forEach((elm) => _element.append(elm));
});
}

_cleanUp() {
void _cleanUp() {
if (_view == null) return;

_view.nodes.forEach((node) => node.remove());
_scope.destroy();
_childScope.destroy();

_view = null;
_scope = null;
_childScope = null;
}

Route get route => _viewRoute;

String get routeName => _viewRoute.name;

Map<String, String> get parameters {
var res = <String, String>{};
var p = _viewRoute;
Expand Down
32 changes: 17 additions & 15 deletions lib/routing/routing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ class RouteViewFactory {

RouteViewFactory(this.locationService);

call(String templateUrl) =>
Function call(String templateUrl) =>
(RouteEnterEvent event) => _enterHandler(event, templateUrl);

_enterHandler(RouteEnterEvent event, String templateUrl,
{List<Module> modules, String templateHtml}) =>
locationService._route(event.route, templateUrl, fromEvent: true,
modules: modules, templateHtml: templateHtml);
void _enterHandler(RouteEnterEvent event, String templateUrl,
{List<Module> modules, String templateHtml}) {
locationService._route(event.route, templateUrl, fromEvent: true,
modules: modules, templateHtml: templateHtml);
}

configure(Map<String, NgRouteCfg> config) =>
_configure(locationService.router.root, config);
void configure(Map<String, NgRouteCfg> config) {
_configure(locationService.router.root, config);
}

_configure(Route route, Map<String, NgRouteCfg> config) {
void _configure(Route route, Map<String, NgRouteCfg> config) {
config.forEach((name, cfg) {
var modulesCalled = false;
List<Module> newModules;
Expand Down Expand Up @@ -115,8 +117,8 @@ typedef void RouteInitializerFn(Router router, RouteViewFactory viewFactory);
class NgRoutingHelper {
final Router router;
final Application _ngApp;
List<NgView> portals = <NgView>[];
Map<String, _View> _templates = new Map<String, _View>();
final _portals = <NgView>[];
final _templates = <String, _View>{};

NgRoutingHelper(RouteInitializer initializer, Injector injector, this.router,
this._ngApp) {
Expand All @@ -136,7 +138,7 @@ class NgRoutingHelper {
router.onRouteStart.listen((RouteStartEvent routeEvent) {
routeEvent.completed.then((success) {
if (success) {
portals.forEach((NgView p) => p._maybeReloadViews());
_portals.forEach((NgView p) => p._maybeReloadViews());
}
});
});
Expand All @@ -155,7 +157,7 @@ class NgRoutingHelper {
if (viewDef == null) continue;
var templateUrl = viewDef.template;

NgView view = portals.lastWhere((NgView v) {
NgView view = _portals.lastWhere((NgView v) {
return _routePath(route) != _routePath(v._route) &&
_routePath(route).startsWith(_routePath(v._route));
}, orElse: () => null);
Expand All @@ -173,11 +175,11 @@ class NgRoutingHelper {
}

void _registerPortal(NgView ngView) {
portals.add(ngView);
_portals.add(ngView);
}

void _unregisterPortal(NgView ngView) {
portals.remove(ngView);
_portals.remove(ngView);
}
}

Expand All @@ -190,7 +192,7 @@ class _View {
}

String _routePath(Route route) {
var path = [];
final path = [];
var p = route;
while (p.parent != null) {
path.insert(0, p.name);
Expand Down

0 comments on commit daef71b

Please sign in to comment.