Browse Source

14-166 Improving observables and use subjects, also cleanup

tags/before_ngrx
Nils Dittberner 8 years ago
parent
commit
17daebad8d
6 changed files with 17 additions and 19 deletions
  1. +1
    -1
      src/app/app.component.html
  2. +1
    -1
      src/app/header/header.component.html
  3. +0
    -2
      src/app/recipes/recipe.service.ts
  4. +2
    -9
      src/app/recipes/recipes.component.ts
  5. +9
    -2
      src/app/shopping-list/shopping-list.component.ts
  6. +4
    -4
      src/app/shopping-list/shopping-list.service.ts

+ 1
- 1
src/app/app.component.html View File

@@ -1,4 +1,4 @@
<app-header (featureSelected)="onNavigate($event)"></app-header>
<app-header></app-header>
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">


+ 1
- 1
src/app/header/header.component.html View File

@@ -1,7 +1,7 @@
<nav class="navbar navbar-default"> <nav class="navbar navbar-default">
<div class="container-fluid"> <div class="container-fluid">
<div class="navbar-header"> <div class="navbar-header">
<a href="#" class="navbar-brand">Recipe Book</a>
<a routerLink="/" class="navbar-brand">Recipe Book</a>
</div> </div>


<div class="navbar-default"> <div class="navbar-default">


+ 0
- 2
src/app/recipes/recipe.service.ts View File

@@ -5,8 +5,6 @@ import { ShoppingListService } from "../shopping-list/shopping-list.service";


@Injectable() @Injectable()
export class RecipeService { export class RecipeService {
recipeSelected = new EventEmitter<Recipe>();

private recipes: Recipe[] = [ private recipes: Recipe[] = [
new Recipe( new Recipe(
'A Test Recipe', 'A Test Recipe',


+ 2
- 9
src/app/recipes/recipes.component.ts View File

@@ -1,6 +1,5 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';


import { Recipe } from './recipe.model';
import { RecipeService } from './recipe.service'; import { RecipeService } from './recipe.service';


@Component({ @Component({
@@ -10,16 +9,10 @@ import { RecipeService } from './recipe.service';
providers: [RecipeService] providers: [RecipeService]
}) })
export class RecipesComponent implements OnInit { export class RecipesComponent implements OnInit {
selectedRecipe: Recipe;

constructor(private recipeService: RecipeService) { }
constructor() { }


ngOnInit() { ngOnInit() {
this.recipeService.recipeSelected.subscribe(
(recipe: Recipe) => {
this.selectedRecipe = recipe;
}
);
} }


} }

+ 9
- 2
src/app/shopping-list/shopping-list.component.ts View File

@@ -1,23 +1,30 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Ingredient } from '../shared/ingredient.model'; import { Ingredient } from '../shared/ingredient.model';
import { ShoppingListService } from './shopping-list.service'; import { ShoppingListService } from './shopping-list.service';
import { Subscription } from 'rxjs/Subscription';
import { OnDestroy } from '@angular/core/src/metadata/lifecycle_hooks';


@Component({ @Component({
selector: 'app-shopping-list', selector: 'app-shopping-list',
templateUrl: './shopping-list.component.html', templateUrl: './shopping-list.component.html',
styleUrls: ['./shopping-list.component.css'] styleUrls: ['./shopping-list.component.css']
}) })
export class ShoppingListComponent implements OnInit {
export class ShoppingListComponent implements OnInit, OnDestroy {
ingredients: Ingredient[]; ingredients: Ingredient[];
private subscription: Subscription


constructor(private shoppingListService: ShoppingListService) { } constructor(private shoppingListService: ShoppingListService) { }


ngOnInit() { ngOnInit() {
this.ingredients = this.shoppingListService.getIngredients(); this.ingredients = this.shoppingListService.getIngredients();
this.shoppingListService.ingredientsChanged.subscribe(
this.subscription = this.shoppingListService.ingredientsChanged.subscribe(
(ingredients: Ingredient[]) => { (ingredients: Ingredient[]) => {
this.ingredients = ingredients; this.ingredients = ingredients;
} }
); );
} }

ngOnDestroy() {
this.subscription.unsubscribe();
}
} }

+ 4
- 4
src/app/shopping-list/shopping-list.service.ts View File

@@ -1,8 +1,8 @@
import { Ingredient } from "../shared/ingredient.model"; import { Ingredient } from "../shared/ingredient.model";
import { EventEmitter } from "@angular/core";
import { Subject } from "rxjs/Subject";


export class ShoppingListService { export class ShoppingListService {
ingredientsChanged = new EventEmitter<Ingredient[]>();
ingredientsChanged = new Subject<Ingredient[]>();
private ingredients: Ingredient[] = [ private ingredients: Ingredient[] = [
new Ingredient('Apples', 5), new Ingredient('Apples', 5),
new Ingredient('Tomatoes', 10) new Ingredient('Tomatoes', 10)
@@ -14,11 +14,11 @@ export class ShoppingListService {


addIngredient(ingredient: Ingredient) { addIngredient(ingredient: Ingredient) {
this.ingredients.push(ingredient); this.ingredients.push(ingredient);
this.ingredientsChanged.emit(this.ingredients.slice());
this.ingredientsChanged.next(this.ingredients.slice());
} }


addIngredients(ingredients: Ingredient[]) { addIngredients(ingredients: Ingredient[]) {
this.ingredients.push(...ingredients); this.ingredients.push(...ingredients);
this.ingredientsChanged.emit(this.ingredients.slice());
this.ingredientsChanged.next(this.ingredients.slice());
} }
} }

Loading…
Cancel
Save