Enforcing Immutability

We cheated a little in the previous example. We told Angular that all of our inputs, including the actor object, were immutable objects, but we went ahead and updated its properties, violating the immutability principle. As a result we ended up with a sync problem between our models and our views. One way to enforce immutability is using the library Immutable.js.

Because in JavaScript primitive types like string and number are immutable by definition, we should only take care of the objects we are using. In this case, the actor object.

Here's an example comparing a mutable type like an array to an immutable type like a string:

  1. var b = ['C', 'a', 'r'];
  2. b[0] = 'B';
  3. console.log(b) // ['B', 'a', 'r'] => The first letter changed, arrays are mutable
  4. var a = 'Car';
  5. a[0] = 'B';
  6. console.log(a); // 'Car' => The first letter didn't change, strings are immutable

First we need to install the immutable.js library using the command:

  1. npm install --save immutable

Then in our AppComponent we import the library and use it to create an actor object as an immutable.

app/app.component.ts

  1. import { Component } from '@angular/core';
  2. import { MovieComponent } from './movie.component';
  3. import * as Immutable from 'immutable';
  4. @Component({
  5. selector: 'app-root',
  6. template: `
  7. <h1>MovieApp</h1>
  8. <p>{{ slogan }}</p>
  9. <button type="button" (click)="changeActor()">
  10. Change Actor
  11. </button>
  12. <app-movie [title]="title" [actor]="actor"></app-movie>`
  13. })
  14. export class AppComponent {
  15. slogan = 'Just movie information';
  16. title = 'Terminator 1';
  17. actor = Immutable.Map({
  18. firstName: 'Arnold',
  19. lastName: 'Schwarzenegger'
  20. })
  21. changeActor() {
  22. this.actor = this.actor.merge({ firstName: 'Nicholas', lastName: 'Cage' });
  23. }
  24. }

Now, instead of creating an instance of an Actor class, we are defining an immutable object using Immutable.Map. Because this.actor is now an immutable object, we cannot change its internal properties (firstName and lastName) directly. What we can do however is create another object based on actor that has different values for both fields - that is exactly what the merge method does.

Because we are always getting a new object when we try to change the actor, there's no point in having two different methods in our component. We removed the methods changeActorProperties and changeActorObject and created a new one called changeActor.

Additional changes have to be made to the MovieComponent as well. First we need to declare the actor object as an immutable type, and in the template, instead of trying to access the object properties directly using a syntax like actor.firstName, we need to use the get method of the immutable.

app/movie.component.ts

  1. import { Component, Input } from '@angular/core';
  2. import { ChangeDetectionStrategy } from '@angular/core';
  3. import * as Immutable from 'immutable';
  4. @Component({
  5. selector: 'app-movie',
  6. template: `
  7. <div>
  8. <h3>{{ title }}</h3>
  9. <p>
  10. <label>Actor:</label>
  11. <span>{{ actor.get('firstName') }} {{ actor.get('lastName') }}</span>
  12. </p>
  13. </div>`,
  14. changeDetection: ChangeDetectionStrategy.OnPush
  15. })
  16. export class MovieComponent {
  17. @Input() title: string;
  18. @Input() actor: Immutable.Map<string, string>;
  19. }

View Example

Using this pattern we are taking full advantage of the "OnPush" change detection strategy and thus reducing the amount of work done by Angular to propagate changes and to get models and views in sync. This improves the performance of the application.

原文: https://angular-2-training-book.rangle.io/handout/change-detection/enforcing_immutability.html