Skip to content

Commit 0e22067

Browse files
committed
feature(routing): make routing use ViewPort
closes dart-archive#704
1 parent d07044e commit 0e22067

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

lib/routing/ng_view.dart

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,22 @@ part of angular.routing;
5858
@NgDirective(
5959
selector: 'ng-view',
6060
publishTypes: const [RouteProvider],
61+
children: NgAnnotation.TRANSCLUDE_CHILDREN,
6162
visibility: NgDirective.CHILDREN_VISIBILITY)
6263
class NgViewDirective implements NgDetachAware, RouteProvider {
6364
final NgRoutingHelper locationService;
6465
final ViewCache viewCache;
6566
final Injector injector;
66-
final Element element;
6767
final Scope scope;
6868
RouteHandle _route;
69+
final ViewPort _viewPort;
6970

7071
View _view;
71-
Scope _scope;
72+
Scope _childScope;
7273
Route _viewRoute;
7374

74-
NgViewDirective(this.element, this.viewCache,
75-
Injector injector, Router router,
76-
this.scope)
75+
NgViewDirective(this.viewCache, Injector injector,
76+
Router router, this.scope, this._viewPort)
7777
: injector = injector,
7878
locationService = injector.get(NgRoutingHelper)
7979
{
@@ -114,29 +114,33 @@ class NgViewDirective implements NgDetachAware, RouteProvider {
114114
}
115115

116116
var newDirectives = viewInjector.get(DirectiveMap);
117+
117118
viewCache.fromUrl(templateUrl, newDirectives).then((viewFactory) {
118119
_cleanUp();
119-
_scope = scope.createChild(new PrototypeMap(scope.context));
120-
_view = viewFactory(
121-
viewInjector.createChild(
122-
[new Module()..value(Scope, _scope)]));
123-
124-
_view.nodes.forEach((elm) => element.append(elm));
120+
_childScope = scope.createChild(new PrototypeMap(scope.context));
121+
var view = _view = viewFactory(viewInjector.createChild(
122+
[new Module()..value(Scope, _childScope)]));
123+
_childScope.rootScope.domWrite(() {
124+
_viewPort.insert(view);
125+
});
125126
});
126127
}
127128

128129
_cleanUp() {
129130
if (_view == null) return;
130131

131-
_view.nodes.forEach((node) => node.remove());
132-
_scope.destroy();
133-
132+
var view = _view;
133+
_childScope.rootScope.domWrite(() {
134+
_viewPort.remove(view);
135+
});
136+
_childScope.destroy();
134137
_view = null;
135-
_scope = null;
138+
_childScope = null;
136139
}
137140

138141
Route get route => _viewRoute;
139142
String get routeName => _viewRoute.name;
143+
140144
Map<String, String> get parameters {
141145
var res = <String, String>{};
142146
var p = _viewRoute;

lib/routing/routing.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ typedef void RouteInitializerFn(Router router, RouteViewFactory viewFactory);
112112
class NgRoutingHelper {
113113
final Router router;
114114
final NgApp _ngApp;
115-
List<NgViewDirective> portals = <NgViewDirective>[];
116-
Map<String, _View> _templates = new Map<String, _View>();
115+
final portals = <NgViewDirective>[];
116+
final _templates = <String, _View>{};
117117

118118
NgRoutingHelper(RouteInitializer initializer, Injector injector, this.router, this._ngApp) {
119119
// TODO: move this to constructor parameters when di issue is fixed:
@@ -153,7 +153,7 @@ class NgRoutingHelper {
153153

154154
NgViewDirective view = portals.lastWhere((NgViewDirective v) {
155155
return _routePath(route) != _routePath(v._route) &&
156-
_routePath(route).startsWith(_routePath(v._route));
156+
_routePath(route).startsWith(_routePath(v._route));
157157
}, orElse: () => null);
158158
if (view != null && !alreadyActiveViews.contains(view)) {
159159
view._show(templateUrl, route, viewDef.modules);

0 commit comments

Comments
 (0)