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

revert(NgView): Reverts "Make use of the ViewPort" #991

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 33 additions & 46 deletions lib/routing/ng_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,49 +58,47 @@ part of angular.routing;
@Decorator(
selector: 'ng-view',
module: NgView.module,
children: Directive.TRANSCLUDE_CHILDREN)
visibility: Directive.CHILDREN_VISIBILITY)
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 Scope _scope;
final NgRoutingHelper locationService;
final ViewCache viewCache;
final Injector injector;
final Element element;
final Scope scope;
RouteHandle _route;

final ViewPort _viewPort;

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


NgView(this._viewCache, Injector injector, Router router,
this._scope, this._viewPort)
: _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);
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);
}

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

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

if (_viewRoute != null) return;
Expand All @@ -115,51 +113,40 @@ 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();
_childScope = _scope.createChild(new PrototypeMap(_scope.context));
_scope = scope.createChild(new PrototypeMap(scope.context));
_view = viewFactory(
viewInjector.createChild([new Module()..bind(Scope, toValue: _childScope)]));

var view = _view;
_scope.rootScope.domWrite(() {
_viewPort.insert(view);
});
viewInjector.createChild([new Module()..bind(Scope, toValue: _scope)]));
_view.nodes.forEach((elm) => element.append(elm));
});
}

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

var view = _view;
var childScope = _childScope;
_scope.rootScope.domWrite(() {
_viewPort.remove(view);
childScope.destroy();
});
_view.nodes.forEach((node) => node.remove());
_scope.destroy();

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

Route get route => _viewRoute;

String get routeName => _viewRoute.name;

Map<String, String> get parameters {
var res = <String, String>{};
var route = _viewRoute;
while (route != null) {
res.addAll(route.parameters);
route = route.parent;
var p = _viewRoute;
while (p != null) {
res.addAll(p.parameters);
p = p.parent;
}
return res;
}
Expand Down
36 changes: 13 additions & 23 deletions test/routing/ng_view_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ main() {
TestBed _;
Router router;

beforeEachModule((Module module) {
module
beforeEachModule((Module m) {
m
..install(new AngularMockModule())
..bind(RouteInitializerFn, toImplementation: FlatRouteInitializer);
});
Expand All @@ -28,22 +28,19 @@ main() {


it('should switch template', async(() {
Element root = _.compile('<div><ng-view></ng-view></div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/foo');
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('Foo');

router.route('/bar');
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('Bar');

router.route('/foo');
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('Foo');
}));

Expand All @@ -59,31 +56,29 @@ main() {

it('should switch template when route is already active', async(() {
// Force the routing system to initialize.
_.compile('<div><ng-view></ng-view></div>');
_.compile('<ng-view></ng-view>');

router.route('/foo');
microLeap();
Element root = _.compile('<div><ng-view></ng-view></div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

microLeap();
_.rootScope.apply();
microLeap();
expect(root.text).toEqual('Foo');
}));


it('should clear template when route is deactivated', async(() {
Element root = _.compile('<div><ng-view></ng-view></div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/foo');
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('Foo');

router.route('/baz'); // route without a template
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('');
}));

Expand All @@ -94,8 +89,8 @@ main() {
TestBed _;
Router router;

beforeEachModule((Module module) {
module
beforeEachModule((Module m) {
m
..install(new AngularMockModule())
..bind(RouteInitializerFn, toImplementation: NestedRouteInitializer);
});
Expand All @@ -116,29 +111,25 @@ main() {
});

it('should switch nested templates', async(() {
Element root = _.compile('<div><ng-view></ng-view></div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/library/all');
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('LibraryBooks');

router.route('/library/1234');
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('LibraryBook 1234');

// nothing should change here
router.route('/library/1234/overview');
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('LibraryBook 1234');

// nothing should change here
router.route('/library/1234/read');
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('LibraryRead Book 1234');
}));
});
Expand All @@ -148,8 +139,8 @@ main() {
TestBed _;
Router router;

beforeEachModule((Module module) {
module
beforeEachModule((Module m) {
m
..install(new AngularMockModule())
..bind(RouteInitializerFn, toValue: (router, views) {
views.configure({
Expand All @@ -166,12 +157,11 @@ main() {
});

it('should switch inline templates', async(() {
Element root = _.compile('<div><ng-view></ng-view></div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/foo');
microLeap();
_.rootScope.apply();
expect(root.text).toEqual('Hello');
}));
});
Expand Down
24 changes: 13 additions & 11 deletions test/routing/routing_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ main() {
_.injector.get(TemplateCache)
.put('foo.html', new HttpResponse(200, '<h1>Foo</h1>'));

Element root = _.compile('<div><ng-view></ng-view><div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/foo');
microLeap();
_.rootScope.apply();

expect(enterCount).toBe(1);
expect(root.text).toEqual('Foo');
}));
Expand Down Expand Up @@ -231,18 +231,18 @@ main() {
_.injector.get(TemplateCache)
.put('foo.html', new HttpResponse(200, '<h1>Foo</h1>'));

Element root = _.compile('<div><ng-view></ng-view><div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/foo');
microLeap();
_.rootScope.apply();

expect(root.text).toEqual('Foo');
expect(leaveCount).toBe(0);

router.route('/bar');
microLeap();
_.rootScope.apply();

expect(root.text).toEqual('');
expect(leaveCount).toBe(1);
}));
Expand All @@ -263,12 +263,12 @@ main() {
_.injector.get(TemplateCache)
.put('foo.html', new HttpResponse(200, '<div make-it-new>Old!</div>'));

Element root = _.compile('<div><ng-view></ng-view><div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/foo');
microLeap();
_.rootScope.apply();

expect(root.text).toEqual('New!');
}));

Expand All @@ -288,12 +288,12 @@ main() {
_.injector.get(TemplateCache)
.put('foo.html', new HttpResponse(200, '<div make-it-new>Old!</div>'));

Element root = _.compile('<div><ng-view></ng-view><div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/foo');
microLeap();
_.rootScope.apply();

expect(root.text).toEqual('New!');
}));

Expand All @@ -313,12 +313,13 @@ main() {
_.injector.get(TemplateCache)
.put('foo.html', new HttpResponse(200, '<div>{{\'World\' | hello}}</div>'));

Element root = _.compile('<div><ng-view></ng-view></div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/foo');
microLeap();
_.rootScope.apply();

expect(root.text).toEqual('Hello, World!');
}));

Expand All @@ -338,12 +339,13 @@ main() {
_.injector.get(TemplateCache)
.put('foo.html', new HttpResponse(200, '<div>{{\'World\' | hello}}</div>'));

Element root = _.compile('<div><ng-view></ng-view></div>');
Element root = _.compile('<ng-view></ng-view>');
expect(root.text).toEqual('');

router.route('/foo');
microLeap();
_.rootScope.apply();

expect(root.text).toEqual('Hello, World!');
}));

Expand Down