chore(examples): extend inbox example code to include sorting

This commit is contained in:
Matias Niemelä 2015-10-09 16:42:00 -07:00 committed by Brian Ford
parent c0b7bae9d3
commit ea661fa10f
2 changed files with 20 additions and 3 deletions

View File

@ -11,7 +11,7 @@ import {
import * as db from './data';
import {ObservableWrapper, PromiseWrapper, Promise} from 'angular2/src/core/facade/async';
import {ListWrapper} from 'angular2/src/core/facade/collection';
import {isPresent} from 'angular2/src/core/facade/lang';
import {isPresent, DateWrapper} from 'angular2/src/core/facade/lang';
class InboxRecord {
id: string = '';
@ -20,7 +20,7 @@ class InboxRecord {
email: string = '';
firstName: string = '';
lastName: string = '';
date: string = '';
date: string;
draft: boolean = false;
constructor(data: {
@ -108,10 +108,21 @@ class InboxCmp {
items: InboxRecord[] = [];
ready: boolean = false;
constructor(public router: Router, db: DbService) {
constructor(public router: Router, db: DbService, params: RouteParams) {
var sortType = params.get('sort');
var sortEmailsByDate = isPresent(sortType) && sortType == "date";
PromiseWrapper.then(db.emails(), emails => {
this.ready = true;
this.items = emails.map(data => new InboxRecord(data));
if (sortEmailsByDate) {
ListWrapper.sort(this.items,
(a, b) => DateWrapper.toMillis(DateWrapper.fromISOString(a.date)) <
DateWrapper.toMillis(DateWrapper.fromISOString(b.date)) ?
-1 :
1);
}
});
}
}

View File

@ -15,4 +15,10 @@
<span class="btn medium primary">
<a [router-link]="record.draft ? ['../Drafts'] : ['../Inbox']" class="back-button">Back</a>
</span>
<hr />
<a [router-link]="['../Inbox', { sort: 'date'} ]" class="sort-button">
View Latest Messages
</a>
</div>