Skip to content

Commit

Permalink
feat(router): Added method to get current instruction
Browse files Browse the repository at this point in the history
This method delegates to the root router to get the current complete instruction.
  • Loading branch information
brandonroberts committed Feb 20, 2016
1 parent e7470d5 commit c1d499d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
20 changes: 20 additions & 0 deletions modules/angular1_router/test/integration/router_spec.js
Expand Up @@ -95,6 +95,26 @@ describe('router', function () {
expect(elt.text()).toBe('Home (true)');
}));

it('should provide the complete current instruction', inject(function($location, $q, $router) {
registerDirective('homeCmp', {
template: 'Home ({{homeCmp.isAdmin}})'
});

registerDirective('app', {
template: '<div ng-outlet></div>',
$routeConfig: [
{ path: '/', component: 'homeCmp', name: 'Home' }
]
});

compile('<app></app>');

$location.path('/');
$rootScope.$digest();
var instruction = $router.generate(['/Home']);
expect($router.currentInstruction()).toEqual(instruction);
}));

function registerDirective(name, options) {
function factory() {
return {
Expand Down
11 changes: 11 additions & 0 deletions modules/angular2/src/router/router.ts
Expand Up @@ -413,6 +413,12 @@ export class Router {
var ancestorInstructions = this._getAncestorInstructions();
return this.registry.generate(linkParams, ancestorInstructions);
}


/**
* Returns the current `Instruction`
*/
currentInstruction(): Instruction { return this._currentInstruction; }
}

@Injectable()
Expand Down Expand Up @@ -500,6 +506,11 @@ class ChildRouter extends Router {
// Delegate navigation to the root router
return this.parent.navigateByInstruction(instruction, _skipLocationChange);
}

currentInstruction(): Instruction {
// Delegate current instruction to the root router
return this.parent.currentInstruction();
}
}


Expand Down
17 changes: 17 additions & 0 deletions modules/angular2/test/router/router_spec.ts
Expand Up @@ -210,6 +210,23 @@ export function main() {
});
}));

it('should provide the complete current instruction', inject([AsyncTestCompleter], (async) => {
var outlet = makeDummyOutlet();

router.registerPrimaryOutlet(outlet)
.then((_) => router.config([
new Route({path: '/a', component: DummyComponent, name: 'A'}),
new Route({path: '/b', component: DummyComponent, name: 'B'})
]))
.then((_) => router.navigateByUrl('/a'))
.then((_) => {
var instruction = router.generate(['/A']);

expect(router.currentInstruction()).toEqual(instruction);
async.done();
});
}));

describe('query string params', () => {
it('should use query string params for the root route', () => {
router.config(
Expand Down

0 comments on commit c1d499d

Please sign in to comment.