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

$router.navigate to component #256

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions src/grammar.ats
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,43 @@ export class Grammar {
return instruction;
}

recognizeComponent(name:string, parentComponentName = '/') {
if (typeof name === 'undefined') {
return;
}

var componentRecognizer = this.rules[parentComponentName];
if (!componentRecognizer) {
return;
}

var handlers = componentRecognizer.handlersFor(name);
if (!handlers) {
return;
}

var handler = handlers[0].handler;

var instruction = {
viewports: {},
params: {},
canonicalUrl: handler.path
};


forEach(handler.components, (componentName, viewportName) => {
instruction.viewports[viewportName] = {
viewports: {}
};
});

forEach(instruction.viewports, (instruction, componentName) => {
instruction.component = handler.components[componentName];
});

return instruction;
}

generate(name, params) {
var path = '';
var solution;
Expand Down Expand Up @@ -183,6 +220,10 @@ class CanonicalRecognizer {
return context;
}

handlersFor(componentName) {
return this.recognizer.handlersFor(componentName);
}

generate(name, params) {
return this.recognizer.generate(name, params);
}
Expand Down
8 changes: 6 additions & 2 deletions src/router.ats
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Router {


/**
* @description Navigate to a URL.
* @description Navigate to a URL or Component.
* Returns the cannonical URL for the route navigated to.
*/
navigate(url) {
Expand All @@ -73,7 +73,7 @@ class Router {

this.lastNavigationAttempt = url;

var instruction = this.recognize(url);
var instruction = this.recognize(url) || this.recognizeComponent(url);

if (!instruction) {
return Promise.reject();
Expand Down Expand Up @@ -161,6 +161,10 @@ class Router {
return this.registry.recognize(url);
}

recognizeComponent(name) {
return this.registry.recognizeComponent(name);
}



/**
Expand Down
12 changes: 12 additions & 0 deletions test/router-viewport.es5.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,18 @@ describe('ngOutlet', function () {
expect($router.navigating).toBe(false);
});

it('Should allow navigating by component name', function () {
compile('<ng-viewport></ng-viewport>');

$router.config([
{ path: '/', component: 'one' }
]);

$router.navigate('one');
$rootScope.$digest();

expect(elt.text()).toBe('one');
});

function registerComponent(name, template, componentConstructor, routeConfig) {
if (!template) {
Expand Down