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

Commit d634f00

Browse files
committed
Refactored examples
1 parent c8e3013 commit d634f00

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

app/components/examples/examples.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
1+
// Angular2 specified stuff
12
import {Component, ViewEncapsulation} from 'angular2/core';
2-
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
3-
import {NavigationCmp} from './navigation';
3+
import {RouteConfig, ROUTER_DIRECTIVES, CanActivate} from 'angular2/router';
4+
5+
// 3rd party libraries
6+
import {tokenNotExpired} from 'angular2-jwt/angular2-jwt';
47

5-
import {AuthorCmp} from './author/author';
6-
import {BookCmp} from './book/book';
8+
// Component specified stuff
9+
import {NavigationCmp} from './navigation';
10+
import {Routes} from './routes';
711

12+
// Component setup
813
@Component({
914
selector: 'examples',
1015
templateUrl: './components/examples/examples.html',
1116
encapsulation: ViewEncapsulation.None,
1217
directives: [ROUTER_DIRECTIVES, NavigationCmp]
1318
})
1419

15-
@RouteConfig([
16-
{path: '/author', component: AuthorCmp, name: 'Author'},
17-
{path: '/book', component: BookCmp, name: 'Book'}
18-
])
20+
// Specify component routes
21+
@RouteConfig(Routes.get())
22+
23+
// This is protected component, so check that user has valid JWT
24+
@CanActivate(() => tokenNotExpired())
1925

26+
// Actual component class
2027
export class ExamplesCmp {}

app/components/examples/navigation.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1+
// Angular2 specified stuff
12
import {Component} from 'angular2/core';
23
import {CORE_DIRECTIVES} from 'angular2/common';
34
import {ROUTER_DIRECTIVES, Location} from 'angular2/router';
45

6+
// Component setup
57
@Component({
68
selector: 'navigation-component',
79
templateUrl: './components/examples/navigation.html',
810
directives: [CORE_DIRECTIVES, ROUTER_DIRECTIVES]
911
})
1012

13+
// Actual component class
1114
export class NavigationCmp {
1215
constructor(
1316
private _location: Location
1417
) {}
1518

16-
isActive(item): boolean {
17-
let regExp = new RegExp(item, 'gi');
19+
/**
20+
* Function to check if specified path is active or not.
21+
*
22+
* @param {string} path
23+
* @returns {boolean}
24+
*/
25+
isActive(path: string): boolean {
26+
let regExp = new RegExp(path, 'gi');
1827

1928
return !!this._location.path().match(regExp);
2029
}

app/components/examples/routes.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Route components
2+
import {AuthorCmp} from './author/author';
3+
import {BookCmp} from './book/book';
4+
5+
export class Routes {
6+
static items = [
7+
{path: '/author', component: AuthorCmp, name: 'Author'},
8+
{path: '/book', component: BookCmp, name: 'Book'}
9+
];
10+
11+
static get(): any[] {
12+
return Routes.items;
13+
}
14+
}

0 commit comments

Comments
 (0)