Skip to content

Commit

Permalink
Add workaround for material bug
Browse files Browse the repository at this point in the history
angular/components#5593
There's a race condition that breaks md-table on route changes. Add a
setTimeout so we trigger a second change detection after onInit.
Also run format
  • Loading branch information
heathkit committed Jul 17, 2017
1 parent 5e17657 commit 4491018
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/app/ice-cream-table/ice-cream-table.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import {DataSource} from '@angular/cdk';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {Observable } from 'rxjs/Observable';
import {GroupedObservable} from 'rxjs/operator/groupBy';
import {Subscription} from 'rxjs/Subscription';
import {Subject} from 'rxjs/Subject';
import { DataSource } from '@angular/cdk';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { GroupedObservable } from 'rxjs/operator/groupBy';
import { Subscription } from 'rxjs/Subscription';
import { Subject } from 'rxjs/Subject';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import {
trigger,
Expand All @@ -28,28 +28,32 @@ const VOTE_DELAY = 750;
templateUrl: './ice-cream-table.component.html',
styleUrls: ['./ice-cream-table.component.css'],
animations: [
trigger('pulse', [
state('in', style({background: 'white'})),
transition('void => *', [
style({background: 'lightseagreen'}),
animate('350ms ease-out')
]),
])
trigger('pulse', [
state('in', style({ background: 'white' })),
transition('void => *', [
style({ background: 'lightseagreen' }),
animate('350ms ease-out')
]),
])
]
})
export class IceCreamTableComponent implements OnInit, OnDestroy {
dataSource: FlavorDataSource | null;
displayedColumns = ['votes', 'image', 'name', 'like'];
private vote = new Subject<any>();
vote$ = this.vote.asObservable();
disabled: {[key: number]: boolean} = {};
disabled: { [key: number]: boolean } = {};

voteSub: Subscription;

constructor(private db: AngularFireDatabase) { }

ngOnInit() {
this.dataSource = new FlavorDataSource(this.db);
// Workaround for https://github.com/angular/material2/issues/5593
setTimeout(() => {
this.dataSource = new FlavorDataSource(this.db);
}, 1);

this.voteSub = this.vote$.groupBy(s => s).subscribe((value: any) => {
const id = value.key;
value.throttleTime(VOTE_DELAY - 250).subscribe(() => {
Expand Down Expand Up @@ -82,8 +86,8 @@ export class FlavorDataSource extends DataSource<any> {

/** Connect function called by the table to retrieve one stream containing the data to render. */
connect(): Observable<FlavorData[]> {
const flavorList = this.db.list('/flavors', {query: {orderByChild: 'votes'}})
.map((array) => array.slice().reverse() ) as FirebaseListObservable<FlavorData[]>;
const flavorList = this.db.list('/flavors', { query: { orderByChild: 'votes' } })
.map((array) => array.slice().reverse()) as FirebaseListObservable<FlavorData[]>;
return flavorList;
}

Expand Down

0 comments on commit 4491018

Please sign in to comment.