Skip to content

Commit cc1264e

Browse files
committed
edit product and pub sub service
- enabled edit product - added pub sub service for communicating between product add/edit component and product list component - refactored animations
1 parent 98e7883 commit cc1264e

17 files changed

+114
-47
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# angular2-animation-tutorial-example
22

3-
Angular 2 / 4 - Router Animation Example & Tutorial
3+
Angular 2/4 - Router Animation Example & Tutorial

app/_animations/fade-in.animation.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { trigger, state, animate, transition, style } from '@angular/animations';
2+
3+
export const fadeInAnimation =
4+
trigger('fadeInAnimation', [
5+
// route 'enter' transition
6+
transition(':enter', [
7+
8+
// styles at start of transition
9+
style({ opacity: 0 }),
10+
11+
// animation and styles at end of transition
12+
animate('.3s', style({ opacity: 1 }))
13+
]),
14+
]);

app/_animations/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './fade-in.animation';
2+
export * from './slide-in-out.animation';

app/_helpers/animations.ts renamed to app/_animations/slide-in-out.animation.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
import { trigger, state, animate, transition, style } from '@angular/animations';
22

3-
export function fadeIn() {
4-
return trigger('fadeIn', [
5-
// route 'enter' transition
6-
transition(':enter', [
7-
8-
// styles at start of transition
9-
style({ opacity: 0 }),
10-
11-
// animation and styles at end of transition
12-
animate('.3s', style({ opacity: 1 }))
13-
]),
14-
]);
15-
}
16-
17-
export function slideInOut() {
18-
return trigger('slideInOut', [
3+
export const slideInOutAnimation =
4+
trigger('slideInOutAnimation', [
195

206
// end state styles for route container (host)
217
state('*', style({
@@ -62,5 +48,4 @@ export function slideInOut() {
6248
backgroundColor: 'rgba(0, 0, 0, 0)'
6349
}))
6450
])
65-
])
66-
}
51+
]);

app/_services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './product.service';
2+
export * from './pub-sub.service';

app/_services/product.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class ProductService {
77
}
88

99
getById(id: number) {
10-
return this.getProducts().filter(product => product.id === id);
10+
return this.getProducts().find(product => product.id === id);
1111
}
1212

1313
save(product: any) {

app/_services/pub-sub.service.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Injectable } from '@angular/core';
2+
import { Observable } from 'rxjs';
3+
import { Subject } from 'rxjs/Subject';
4+
5+
@Injectable()
6+
export class PubSubService {
7+
private subjects: Subject<any>[] = [];
8+
9+
publish(eventName: string) {
10+
// ensure a subject for the event name exists
11+
this.subjects[eventName] = this.subjects[eventName] || new Subject<any>();
12+
13+
// publish event
14+
this.subjects[eventName].next();
15+
}
16+
17+
on(eventName: string): Observable<any> {
18+
// ensure a subject for the event name exists
19+
this.subjects[eventName] = this.subjects[eventName] || new Subject<any>();
20+
21+
// return observable
22+
return this.subjects[eventName].asObservable();
23+
}
24+
}

app/app-routing.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const routes: Routes = [
1010
path: 'products',
1111
component: ProductListComponent,
1212
children: [
13-
{ path: 'add', component: ProductAddEditComponent }
13+
{ path: 'add', component: ProductAddEditComponent },
14+
{ path: 'edit/:id', component: ProductAddEditComponent }
1415
]
1516
},
1617

app/app.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
55

66
import { AppComponent } from './app.component';
77
import { AppRoutingModule, routedComponents } from './app-routing.module';
8-
import { ProductService } from './_services/index';
8+
import { ProductService, PubSubService } from './_services/index';
99

1010
@NgModule({
1111
imports: [
@@ -19,7 +19,8 @@ import { ProductService } from './_services/index';
1919
routedComponents
2020
],
2121
providers: [
22-
ProductService
22+
ProductService,
23+
PubSubService
2324
],
2425
bootstrap: [AppComponent]
2526
})

app/home/home.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<h1>Home</h1>
2-
<p>Angular 2 (version 4) Animation Tutorial & Example</p>
2+
<p>Angular 2/4 - Animation Tutorial & Example</p>

0 commit comments

Comments
 (0)