fix(router): do not reuse common children with different parents

This commit is contained in:
Brian Ford 2015-09-09 12:00:31 -07:00
parent 8aec215ca0
commit 77e8304fc4
2 changed files with 28 additions and 2 deletions

View File

@ -248,7 +248,7 @@ export class Router {
return this._outlet.canReuse(instruction.component) return this._outlet.canReuse(instruction.component)
.then((result) => { .then((result) => {
instruction.component.reuse = result; instruction.component.reuse = result;
if (isPresent(this._childRouter) && isPresent(instruction.child)) { if (result && isPresent(this._childRouter) && isPresent(instruction.child)) {
return this._childRouter._canReuse(instruction.child); return this._childRouter._canReuse(instruction.child);
} }
}); });

View File

@ -35,6 +35,7 @@ import {RouteRegistry} from 'angular2/src/router/route_registry';
import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver'; import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver';
var cmpInstanceCount; var cmpInstanceCount;
var childCmpInstanceCount;
var log: string[]; var log: string[];
export function main() { export function main() {
@ -57,6 +58,7 @@ export function main() {
beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => { beforeEach(inject([TestComponentBuilder, Router], (tcBuilder, router) => {
tcb = tcBuilder; tcb = tcBuilder;
rtr = router; rtr = router;
childCmpInstanceCount = 0;
cmpInstanceCount = 0; cmpInstanceCount = 0;
log = []; log = [];
})); }));
@ -147,6 +149,27 @@ export function main() {
}); });
})); }));
it('should not reuse children when parent components change',
inject([AsyncTestCompleter], (async) => {
compile()
.then((_) => rtr.config([new Route({path: '/team/:id/...', component: TeamCmp})]))
.then((_) => rtr.navigate('/team/angular/user/rado'))
.then((_) => {
rootTC.detectChanges();
expect(cmpInstanceCount).toBe(1);
expect(childCmpInstanceCount).toBe(1);
expect(rootTC.nativeElement).toHaveText('team angular { hello rado }');
})
.then((_) => rtr.navigate('/team/dart/user/rado'))
.then((_) => {
rootTC.detectChanges();
expect(cmpInstanceCount).toBe(2);
expect(childCmpInstanceCount).toBe(2);
expect(rootTC.nativeElement).toHaveText('team dart { hello rado }');
async.done();
});
}));
it('should inject route data into component', inject([AsyncTestCompleter], (async) => { it('should inject route data into component', inject([AsyncTestCompleter], (async) => {
compile() compile()
.then((_) => rtr.config([ .then((_) => rtr.config([
@ -256,7 +279,10 @@ class RouteDataCmp {
@View({template: "hello {{user}}"}) @View({template: "hello {{user}}"})
class UserCmp { class UserCmp {
user: string; user: string;
constructor(params: RouteParams) { this.user = params.get('name'); } constructor(params: RouteParams) {
childCmpInstanceCount += 1;
this.user = params.get('name');
}
} }