Angular 2 brings many new concepts that can can improve our JavaScript applications. The first new concept to Angular is the use of Observables. Observables are a proposed feature coming to the JavaScript specification. I wont go in depth into Observables but will just cover some of the high level concepts. If you want a introduction to Observables check out my screen cast.

Intro to RxJS Observables and Angular 2

The rest of this post will cover more data and application state management in a Angular 2 application. This post has been updated to the latest stable 2.0 release of Angular.

Observables can help manage async data and a few other useful patterns. Observables are similar to Promises but with a few key differences. The first is Observables emit multiple values over time. For example a Promise once called will always return one value or one error. This is great until you have multiple values over time. Web socket/real-time based data or event handlers can emit multiple values over any given time. This is where Observables really shine. Observables are used extensively in Angular 2. The new HTTP service and EventEmitter system are all Observable based. Lets look at an example where we subscribe to an Observable.



todosService.todos.subscribe(updatedTodos => {
  this.componentTodos = updatedTodos;
});

// OR if using the prefered async pipe 
// https://angular.io/docs/ts/latest/guide/pipes.html
this.todos = todosService.todos;


In this snippet our todos property on our data service is an Observable. We can subscribe to this Observable in our component. Each time there is a new value emitted from our Observable Angular updates the view.



<!-- Async pipe is used to bind an observable directly in our template -->
<div *ngFor="let todo of todos | async">
  {{ todo.value }} <button (click)="deleteTodo(todo.id)">x</button>
</div>


Observables are treated like arrays. Each value over time is one item in the array. This allows us to use array like methods called operators on our Observable such as map, flatmap, reduce, ect. In our service we will be using a special type of an Observable called a BehaviorSubject. A BehaviorSubject allows us to push and pull values to the underlying Observable. We will see how this will help us construct our service.

In Angular 2 we use RxJS a polyfill/util library for the proposed Observables primitive in the next new version JavaScript. RxJS version 5 is in beta and is a peer dependency with Angular 2. A slim Observable is used in Angular 2 core. The slim Observable does not have many of the useful operators that makes RxJS so productive. The Observable in Angular 2 is slim to keep the byte site of the library down. To use extra operators we import them like so: import 'rxjs/add/operator/map';.

In our example we will have a TodosService. Our todos service will have basic CRUD operations and a Observable stream to subscribe to. This example we will use a REST based API but it could be converted to a real-time socket based API with little effort. Next lets take a look at a the constructor of our service.



@Injectable() 
export class TodoService {
  private _todos: BehaviorSubject<Todo[]>; 
  private baseUrl: string;
  private dataStore: {  // This is where we will store our data in memory
    todos: Todo[]
  };
    
  // Using Angular DI we use the HTTP service
  constructor(private http: Http) {
    this.baseUrl  = 'http://56e05c3213da80110013eba3.mockapi.io/api';
    this.dataStore = { todos: [] };
    this._todos = <BehaviorSubject<Todo[]>>new BehaviorSubject();
  }

  get todos() {
    return this._todos.asObservable();
  }
}


In our todo service we have a few moving parts. First we have a private data store. This is where we store our list of todos in memory. We can return this list immediately for faster rendering or when off-line. For now its simply just holds onto our list of todos. Since services in Angular 2 are singletons we can use them to hold our data model/state we want to share.

Next is our todos BehaviorSubject. Our BehaviorSubject can recieve and emit new Todo lists. We don’t want subscribers of our service to be able to push new values to our subject without going through our CRUD methods. To prevent the data from being altered ouside the service we expose the BehaviorSubject through a public property and cast it to an Observable using the asObservable operator. This will allow components to receive updates but not push new values. We will see how that is done in further in the example. Note a BehaviorSubject is like the Subject class but requires a inital starting value.

Now in our public methods we can load, create, update, and remove todos. Lets start off with loading the todos.



@Injectable() 
export class TodoService {
  private _todos: BehaviorSubject<Todo[]>; 
  private baseUrl: string;
  private dataStore: {
    todos: Todo[]
  };
    
  constructor(private http: Http) {
    this.baseUrl  = 'http://56e05c3213da80110013eba3.mockapi.io/api';
    this.dataStore = { todos: [] };
    this._todos = <BehaviorSubject<Todo[]>>new BehaviorSubject([]);
  }
  
  get todos() {
    return this._todos.asObservable();
  }
    
  loadAll() {
    this.http.get(`${this.baseUrl}/todos`).map(response => response.json()).subscribe(data => {
      this.dataStore.todos = data;
      this._todos.next(Object.assign({}, this.dataStore).todos);
    }, error => console.log('Could not load todos.'));
  }
}


Notice instead of these methods returning new values of our todos list they update our internal data store. We make a copy using Object.assign() so we pass back a new copy of todos and don’t accidentally pass a reference to the original data store.



  // Push a new copy of our todo list to all Subscribers.
  this._todos.next(Object.assign({}, this.dataStore).todos);


Once the data store of todos is updated we push the new list of todos with our private _todos BehaviorSubject. Now anytime we call one of these methods any component subscribed to our public todos Observable stream will get a value pushed down and always have the latest version of the data. This helps keeps our data consistent across our application.

In our component’s ngOnInit method we subscribe to the todos data stream then call load() to load the latest into the stream. The ngOnInit is the ideal place for loading in data. You can read into the docs and the various reasons why this is a best practice.



// In our todo component
ngOnInit() {
  this.todos = this.todoService.todos; // subscribe to entire collection
  this.singleTodo$ = this.todoService.todos
                         .map(todos => todos.find(item => item.id === '1'));  
                         // subscribe to only one todo 
    
  this.todoService.loadAll();    // load all todos
  this.todoService.load('1');    // load only todo with id of '1'
}

We could subsequently call remove and our stream will get a new list with one less todo. So lets add the rest of the code to add CRUD operations to our todos. Now lets look at the todos service in its entirety.



import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import 'rxjs/add/operator/map';

export interface Todo {
  id: number | string;
  createdAt: number;
  value: string;
}

@Injectable()
export class TodoService {
  todos: Observable<Todo[]>
  private _todos: BehaviorSubject<Todo[]>;
  private baseUrl: string;
  private dataStore: {
    todos: Todo[]
  };

  constructor(private http: Http) {
    this.baseUrl = 'http://56e05c3213da80110013eba3.mockapi.io/api';
    this.dataStore = { todos: [] };
    this._todos = <BehaviorSubject<Todo[]>>new BehaviorSubject([]);
    this.todos = this._todos.asObservable();
  }

  loadAll() {
    this.http.get(`${this.baseUrl}/todos`).map(response => response.json()).subscribe(data => {
      this.dataStore.todos = data;
      this._todos.next(Object.assign({}, this.dataStore).todos);
    }, error => console.log('Could not load todos.'));
  }

  load(id: number | string) {
    this.http.get(`${this.baseUrl}/todos/${id}`).map(response => response.json()).subscribe(data => {
      let notFound = true;

      this.dataStore.todos.forEach((item, index) => {
        if (item.id === data.id) {
          this.dataStore.todos[index] = data;
          notFound = false;
        }
      });

      if (notFound) {
        this.dataStore.todos.push(data);
      }

      this._todos.next(Object.assign({}, this.dataStore).todos);
    }, error => console.log('Could not load todo.'));
  }

  create(todo: Todo) {
    this.http.post(`${this.baseUrl}/todos`, JSON.stringify(todo))
      .map(response => response.json()).subscribe(data => {
        this.dataStore.todos.push(data);
        this._todos.next(Object.assign({}, this.dataStore).todos);
      }, error => console.log('Could not create todo.'));
  }

  update(todo: Todo) {
    this.http.put(`${this.baseUrl}/todos/${todo.id}`, JSON.stringify(todo))
      .map(response => response.json()).subscribe(data => {
        this.dataStore.todos.forEach((t, i) => {
          if (t.id === data.id) { this.dataStore.todos[i] = data; }
        });

        this._todos.next(Object.assign({}, this.dataStore).todos);
      }, error => console.log('Could not update todo.'));
  }

  remove(todoId: number) {
    this.http.delete(`${this.baseUrl}/todos/${todoId}`).subscribe(response => {
      this.dataStore.todos.forEach((t, i) => {
        if (t.id === todoId) { this.dataStore.todos.splice(i, 1); }
      });

      this._todos.next(Object.assign({}, this.dataStore).todos);
    }, error => console.log('Could not delete todo.'));
  }
}


Overview

This pattern can also be used in Angular 1. RxJS and Observables are not just an Angular 2 feature. This may seem like a lot of work for a simple todo app but scale this up to a very large app and Observables can really help manage our data and application state. This pattern follows the idea of unidirectional data flow. Meaning data flow is predictable and consistently comes from one source. If you have worked with Flux/Redux based architectures this may seem very familiar.

Diagram of Angular 2 Data flow with Observables
Angular 2 data flow with Observables

This pattern can ensure data is coming from one place in our application and that every component receives the latest version of that data through our data streams. Our component logic simple by just subscribing to public data streams on our data services. A link to the working demo of a Observable data service can be found below. This service conforms to a REST based backend but could easily translate to a socket based service like Firebase without having to change any components.

To get more in depth review of Observables check out Rob Wormald’s intro to Observables or Ben Lesh’s RxJS In-Depth.

Demo