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

Commit aea9a8c

Browse files
committed
Implemented "resolve" for books.
1 parent 80aa5d7 commit aea9a8c

File tree

4 files changed

+66
-23
lines changed

4 files changed

+66
-23
lines changed

app/bootstrap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import {AppCmp} from './components/app/app';
1313
// Bootstrap application
1414
bootstrap(AppCmp, [
1515
ROUTER_PROVIDERS, HTTP_PROVIDERS,
16+
AuthHttp,
1617
provide(LocationStrategy, { useClass: HashLocationStrategy }),
1718
provide(AuthConfig, {
1819
useFactory: () => {
1920
return new AuthConfig();
2021
}
21-
}),
22-
AuthHttp
22+
})
2323
]);

app/components/examples/book/book.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Angular2 specified stuff
2-
import {Component} from 'angular2/core';
3-
import {Headers} from 'angular2/http';
2+
import {Component, Injector, provide} from 'angular2/core';
3+
import {HTTP_PROVIDERS} from 'angular2/http';
4+
import {ComponentInstruction, CanActivate, OnActivate} from 'angular2/router';
45

56
// 3rd party libraries
6-
import {AuthHttp} from 'angular2-jwt/angular2-jwt';
7+
import {AuthHttp, AuthConfig} from 'angular2-jwt/angular2-jwt';
8+
9+
import {BookService} from './service';
710

811
// Component setup
912
@Component({
@@ -12,24 +15,32 @@ import {AuthHttp} from 'angular2-jwt/angular2-jwt';
1215
providers: [AuthHttp]
1316
})
1417

15-
// Actual component class
16-
export class BookCmp {
17-
books: any[];
18+
@CanActivate((next) => {
19+
var injector = Injector.resolveAndCreate([
20+
HTTP_PROVIDERS, BookService, AuthHttp,
21+
provide(AuthConfig, {
22+
useFactory: () => {
23+
return new AuthConfig();
24+
}
25+
})
26+
]);
1827

19-
private apiUrl = '<%= BACKEND_URL %>';
28+
var bookService = injector.get(BookService);
2029

21-
constructor(
22-
private _authHttp: AuthHttp
23-
) {
24-
let headers = new Headers();
30+
return bookService.getBooks().then(
31+
data => {
32+
next.params.books = data;
2533

26-
headers.append('Content-Type', 'application/json');
34+
return true;
35+
}
36+
);
37+
})
38+
39+
// Actual component class
40+
export class BookCmp implements OnActivate {
41+
books: any[];
2742

28-
this._authHttp.get(this.apiUrl + '/book?populate=author', {headers: headers})
29-
.subscribe(
30-
data => this.books = data.json(),
31-
err => console.log(err),
32-
() => console.log('Request Complete')
33-
);
43+
routerOnActivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction) {
44+
this.books = nextInstruction.params.books;
3445
}
3546
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Angular2 specified stuff
2+
import {Headers} from 'angular2/http';
3+
import {Injectable} from 'angular2/core';
4+
5+
// 3rd party libraries
6+
import {AuthHttp} from 'angular2-jwt/angular2-jwt';
7+
8+
@Injectable()
9+
export class BookService {
10+
private apiUrl = '<%= BACKEND_URL %>';
11+
12+
constructor(private _authHttp: AuthHttp) {}
13+
14+
/**
15+
*
16+
* @returns {Promise|Promise<T>}
17+
*/
18+
getBooks() {
19+
return new Promise((resolve, reject) => {
20+
let headers = new Headers();
21+
22+
headers.append('Content-Type', 'application/json');
23+
24+
this._authHttp.get(this.apiUrl + '/book?populate=author', {headers: headers})
25+
.subscribe(
26+
data => resolve(data.json()),
27+
err => reject(err),
28+
() => console.log('Request Complete')
29+
);
30+
});
31+
}
32+
}

tools/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import {normalize, join} from 'path';
77
export const PROJECT_ROOT = normalize(join(__dirname, '..'));
88
export const ENV = argv['env'] || 'dev';
99
export const DEBUG = argv['debug'] || false;
10-
export const PORT = argv['port'] || 5555;
11-
export const LIVE_RELOAD_PORT = argv['reload-port'] || 4002;
10+
export const PORT = argv['port'] || 3000;
11+
export const LIVE_RELOAD_PORT = argv['reload-port'] || 3001;
1212
export const DOCS_PORT = argv['docs-port'] || 4003;
1313
export const APP_BASE = argv['base'] || '/';
1414

15-
export const BACKEND_URL = argv['backend-url'] || 'http://10.1.1.177:1339';
15+
export const BACKEND_URL = argv['backend-url'] || 'http://wunder.sytes.net:1339';
1616

1717
export const ENABLE_HOT_LOADING = !!argv['hot-loader'];
1818
export const HOT_LOADER_PORT = 5578;

0 commit comments

Comments
 (0)