Skip to content

Commit a99031e

Browse files
authored
Contact details in Aurelia
Create a contact detail page in Aurelia. Includes using the router to create links as well as routing with a parameter.
1 parent 19caebd commit a99031e

File tree

8 files changed

+70
-18
lines changed

8 files changed

+70
-18
lines changed

ASP.NET Core Basics/src/Angular/wwwroot/dist/main-client.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ASP.NET Core Basics/src/Angular/wwwroot/dist/main-client.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ASP.NET Core Basics/src/Aurelia/ClientApp/app/components/app/app.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export class App {
3535
moduleId: '../contacts/contactList',
3636
nav: true,
3737
title: 'Contact List'
38+
},
39+
{
40+
route: 'contact-detail/:id',
41+
name: 'contactdetail',
42+
moduleId: '../contacts/contactDetail',
43+
nav: false,
44+
title: 'Contact Detail'
3845
}]);
3946

4047
this.router = router;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<h1>Contact Details</h1>
3+
<hr />
4+
<div if.bind="contact">
5+
<dl class="dl-horizontal">
6+
<dt>ID</dt>
7+
<dd>${contact.id}</dd>
8+
<dt>Name</dt>
9+
<dd>${contact.name}</dd>
10+
<dt>Address</dt>
11+
<dd>${contact.getAddress()}</dd>
12+
<dt>Phone</dt>
13+
<dd>${contact.phone}</dd>
14+
<dt>Email</dt>
15+
<dd>${contact.email}</dd>
16+
</dl>
17+
</div>
18+
<a route-href="route: contactlist">Back to List</a>
19+
<hr />
20+
</template>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { inject } from 'aurelia-framework';
2+
import { Contact } from './contact';
3+
import { ContactService } from './contactService';
4+
5+
@inject(ContactService)
6+
export class ContactDetail {
7+
contact: Contact;
8+
9+
constructor(private contactService: ContactService) { }
10+
11+
activate(parms, routeConfig) {
12+
return this.contactService.getById(parms.id)
13+
.then(contact => this.contact = contact);
14+
}
15+
16+
}
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
<template>
1+
<template>
22
<h1>Contact List</h1>
33

44
<p if.bind="!contacts"><em>Loading...</em></p>
55

66
<table class="table" if.bind="contacts">
77
<thead>
8-
<tr>
9-
<th>ID</th>
10-
<th>Name</th>
11-
<th>Address</th>
12-
<th>Phone</th>
13-
<th>Email</th>
14-
</tr>
8+
<tr>
9+
<th>IDs</th>
10+
<th>Name</th>
11+
<th></th>
12+
</tr>
1513
</thead>
1614
<tbody>
17-
<tr repeat.for="contact of contacts">
18-
<td>${contact.id}</td>
19-
<td>${contact.name}</td>
20-
<td>${contact.getAddress()}</td>
21-
<td>${contact.phone}</td>
22-
<td>${contact.email}</td>
23-
</tr>
15+
<tr repeat.for="contact of contacts" class="${contact.id === selectedContactId ? 'active' : ''" }>
16+
<td>${contact.id}</td>
17+
<td>${contact.name}</td>
18+
<td><a route-href="route: contactdetail; params.bind: {id:contact.id}" click.delegate="select($contact)">Details</a></td>
19+
</tr>
2420
</tbody>
2521
</table>
22+
<hr />
2623
</template>

ASP.NET Core Basics/src/Aurelia/ClientApp/app/components/contacts/contactList.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import { ContactService } from './contactService';
55
@inject(ContactService)
66
export class ContactList {
77
contacts: Contact[];
8+
selectedContactId: number = null;
89

910
constructor(private contactService: ContactService) {}
1011

1112
created() {
1213
this.contactService.getAll()
1314
.then(contacts => this.contacts = contacts);
1415
}
16+
17+
select(contact) {
18+
this.selectedContactId = contact.id;
19+
}
1520
}

ASP.NET Core Basics/src/Aurelia/ClientApp/app/components/contacts/contactService.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ export class ContactService {
1919
.catch(error => console.log(error));
2020
}
2121

22+
getById(id: string): Promise<Contact> {
23+
return this.http.fetch(id)
24+
.then(response => response.json())
25+
.then(contact => new Contact(contact))
26+
.catch(error => console.log(error));
27+
}
28+
2229
}

0 commit comments

Comments
 (0)