Skip to content

Commit 6dce4f4

Browse files
brandonrobertsvikerman
authored andcommitted
feat(router): Added method to get current instruction
This method delegates to the root router to get the current complete instruction.
1 parent 15e1614 commit 6dce4f4

File tree

4 files changed

+81
-16
lines changed

4 files changed

+81
-16
lines changed

modules/angular1_router/src/ng_route_shim.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246
var paramsObj = {};
247247

248248
$rootScope.$on('$routeChangeSuccess', function () {
249-
var newParams = $rootRouter._currentInstruction && $rootRouter._currentInstruction.component.params;
249+
var newParams = $rootRouter.currentInstruction && $rootRouter.currentInstruction.component.params;
250250

251251
angular.forEach(paramsObj, function (val, name) {
252252
delete paramsObj[name];

modules/angular1_router/test/integration/router_spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,46 @@ describe('router', function () {
120120
expect($routerOnActivate).toHaveBeenCalled();
121121
}));
122122

123+
it('should provide the current instruction', inject(function($location, $q) {
124+
registerComponent('homeCmp', {
125+
template: 'Home ({{homeCmp.isAdmin}})'
126+
});
127+
128+
registerComponent('app', {
129+
template: '<div ng-outlet></div>',
130+
$routeConfig: [
131+
{ path: '/', component: 'homeCmp', name: 'Home' }
132+
]
133+
});
134+
compile('<app></app>');
135+
136+
$location.path('/');
137+
$rootScope.$digest();
138+
var instruction = $rootRouter.generate(['/Home']);
139+
expect($rootRouter.currentInstruction).toEqual(instruction);
140+
}));
141+
142+
it('should provide the root level router', inject(function($location, $q) {
143+
registerComponent('homeCmp', {
144+
template: 'Home ({{homeCmp.isAdmin}})',
145+
bindings: {
146+
$router: '<'
147+
}
148+
});
149+
150+
registerComponent('app', {
151+
template: '<div ng-outlet></div>',
152+
$routeConfig: [
153+
{ path: '/', component: 'homeCmp', name: 'Home' }
154+
]
155+
});
156+
compile('<app></app>');
157+
158+
$location.path('/');
159+
$rootScope.$digest();
160+
var homeElement = elt.find('home-cmp');
161+
expect(homeElement.isolateScope().$ctrl.$router.root).toEqual($rootRouter);
162+
}));
123163

124164
function registerDirective(name, options) {
125165
function factory() {

modules/angular2/src/router/router.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ let _resolveToFalse = PromiseWrapper.resolve(false);
3838
export class Router {
3939
navigating: boolean = false;
4040
lastNavigationAttempt: string;
41-
42-
private _currentInstruction: Instruction = null;
41+
/**
42+
* The current `Instruction` for the router
43+
*/
44+
public currentInstruction: Instruction = null;
4345

4446
private _currentNavigation: Promise<any> = _resolveToTrue;
4547
private _outlet: RouterOutlet = null;
@@ -50,8 +52,8 @@ export class Router {
5052
private _subject: EventEmitter<any> = new EventEmitter();
5153

5254

53-
constructor(public registry: RouteRegistry, public parent: Router, public hostComponent: any) {}
54-
55+
constructor(public registry: RouteRegistry, public parent: Router, public hostComponent: any,
56+
public root?: Router) {}
5557

5658
/**
5759
* Constructs a child router. You probably don't need to use this unless you're writing a reusable
@@ -83,8 +85,8 @@ export class Router {
8385
}
8486

8587
this._outlet = outlet;
86-
if (isPresent(this._currentInstruction)) {
87-
return this.commit(this._currentInstruction, false);
88+
if (isPresent(this.currentInstruction)) {
89+
return this.commit(this.currentInstruction, false);
8890
}
8991
return _resolveToTrue;
9092
}
@@ -119,8 +121,8 @@ export class Router {
119121
router._outlet = outlet;
120122

121123
var auxInstruction;
122-
if (isPresent(this._currentInstruction) &&
123-
isPresent(auxInstruction = this._currentInstruction.auxInstruction[outletName])) {
124+
if (isPresent(this.currentInstruction) &&
125+
isPresent(auxInstruction = this.currentInstruction.auxInstruction[outletName])) {
124126
return router.commit(auxInstruction);
125127
}
126128
return _resolveToTrue;
@@ -137,8 +139,8 @@ export class Router {
137139
router = router.parent;
138140
instruction = instruction.child;
139141
}
140-
return isPresent(this._currentInstruction) &&
141-
this._currentInstruction.component == instruction.component;
142+
return isPresent(this.currentInstruction) &&
143+
this.currentInstruction.component == instruction.component;
142144
}
143145

144146

@@ -287,7 +289,7 @@ export class Router {
287289
}
288290

289291
private _canActivate(nextInstruction: Instruction): Promise<boolean> {
290-
return canActivateOne(nextInstruction, this._currentInstruction);
292+
return canActivateOne(nextInstruction, this.currentInstruction);
291293
}
292294

293295
private _routerCanDeactivate(instruction: Instruction): Promise<boolean> {
@@ -324,7 +326,7 @@ export class Router {
324326
* Updates this router and all descendant routers according to the given instruction
325327
*/
326328
commit(instruction: Instruction, _skipLocationChange: boolean = false): Promise<any> {
327-
this._currentInstruction = instruction;
329+
this.currentInstruction = instruction;
328330

329331
var next: Promise<any> = _resolveToTrue;
330332
if (isPresent(this._outlet) && isPresent(instruction.component)) {
@@ -403,10 +405,10 @@ export class Router {
403405
}
404406

405407
private _getAncestorInstructions(): Instruction[] {
406-
var ancestorInstructions = [this._currentInstruction];
408+
var ancestorInstructions = [this.currentInstruction];
407409
var ancestorRouter: Router = this;
408410
while (isPresent(ancestorRouter = ancestorRouter.parent)) {
409-
ancestorInstructions.unshift(ancestorRouter._currentInstruction);
411+
ancestorInstructions.unshift(ancestorRouter.currentInstruction);
410412
}
411413
return ancestorInstructions;
412414
}
@@ -443,6 +445,7 @@ export class RootRouter extends Router {
443445
constructor(registry: RouteRegistry, location: Location,
444446
@Inject(ROUTER_PRIMARY_COMPONENT) primaryComponent: Type) {
445447
super(registry, null, primaryComponent);
448+
this.root = this;
446449
this._location = location;
447450
this._locationSub = this._location.subscribe((change) => {
448451
// we call recognize ourselves
@@ -503,7 +506,7 @@ export class RootRouter extends Router {
503506

504507
class ChildRouter extends Router {
505508
constructor(parent: Router, hostComponent) {
506-
super(parent.registry, parent, hostComponent);
509+
super(parent.registry, parent, hostComponent, parent.root);
507510
this.parent = parent;
508511
}
509512

modules/angular2/test/router/router_spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,28 @@ export function main() {
218218
});
219219
}));
220220

221+
it('should provide the current instruction', inject([AsyncTestCompleter], (async) => {
222+
var outlet = makeDummyOutlet();
223+
224+
router.registerPrimaryOutlet(outlet)
225+
.then((_) => router.config([
226+
new Route({path: '/a', component: DummyComponent, name: 'A'}),
227+
new Route({path: '/b', component: DummyComponent, name: 'B'})
228+
]))
229+
.then((_) => router.navigateByUrl('/a'))
230+
.then((_) => {
231+
var instruction = router.generate(['/A']);
232+
233+
expect(router.currentInstruction).toEqual(instruction);
234+
async.done();
235+
});
236+
}));
237+
238+
it('should provide the root level router from child routers', () => {
239+
let childRouter = router.childRouter(DummyComponent);
240+
expect(childRouter.root).toBe(router);
241+
});
242+
221243
describe('query string params', () => {
222244
it('should use query string params for the root route', () => {
223245
router.config(

0 commit comments

Comments
 (0)