[Angular 2] Refactoring mutations to enforce immutable data in Angular 2
When a Todo property updates, you still must create a new Array of Todos and assign a new reference. This lesson walks you through refactoring from the current approach to the immutable approach.
TodoItemRender.ts:
import {Component, Input, ViewEncapsulation, EventEmitter, Output} from 'angular2/core';
@Component({
selector: 'todo-item-renderer',
// encapsulation:ViewEncapsulation.Emulated, //Default, (parent style pass )in and no (child style go) out
// encapsulation:ViewEncapsulation.Native, // no in and no out
encapsulation:ViewEncapsulation.None, // in and out
template: `
<style>
.completed{
text-decoration: line-through;
}
</style>
<div>
<span [ngClass]="todo.status"
[contentEditable]="todo.isEdit">{{todo.title}}</span>
<button (click)="toggleTodo.emit(todo)">Toggle</button>
<button (click)="todo.edit()">Edit</button>
</div>
`
})
export class TodoItemRenderer{
@Input() todo;
@Output() toggleTodo = new EventEmitter();
}
Here we use EventEmitter to emit a event called 'toggleTodo'. Parent component (TodoList.ts) will catch the event and update the TodoService's todos array.
And todos array should be a new reference.
TodoList.ts:
import {Component} from 'angular2/core';
import {TodoService} from './TodoService';
import {TodoItemRenderer} from './TodoItemRenderer';
import {StartedPipe} from './started-pipe';
@Component({
selector: 'todo-list',
pipes: [StartedPipe],
directives: [TodoItemRenderer],
template: `
<ul>
<li *ngFor="#todo of todoService.todos | started">
<todo-item-renderer [todo]="todo"
(toggleTodo) = "todoService.toggleTodo($event)" // Contains the todoModule
></todo-item-renderer>
</li>
</ul>
`
})
export class TodoList{
constructor(public todoService: TodoService){
}
}
TodoService.ts:
import {TodoModule} from './TodoModule';
export class TodoService {
todos = [
//init some todos
new TodoModule("eat"),
new TodoModule("sleep"),
new TodoModule("code")
];
addNewTodo(todo) {
this.todos = [...this.todos, todo];
}
toggleTodo(todo) {
const i = this.todos.indexOf(todo);
todo.toggle();
this.todos = [
...this.todos.slice(0, i),
todo,
...this.todos.slice( i + 1)
];
}
}
---------------------
[Angular 2] Refactoring mutations to enforce immutable data in Angular 2的更多相关文章
- [Immutable + AngularJS] Use Immutable .List() for Angular array
const stores = Immutable.List([ { name: 'Store42', position: { latitude: 61.45, longitude: 23.11, }, ...
- [Javascript] Simplify Creating Immutable Data Trees With Immer
Immer is a tiny library that makes it possible to work with immutable data in JavaScript in a much m ...
- [Angular2 Router] Resolving route data in Angular 2
From Article: RESOLVING ROUTE DATA IN ANGULAR 2 Github If you know Anuglar UI router, you must know ...
- Angular 1 深度解析:脏数据检查与 angular 性能优化
TL;DR 脏检查是一种模型到视图的数据映射机制,由 $apply 或 $digest 触发. 脏检查的范围是整个页面,不受区域或组件划分影响 使用尽量简单的绑定表达式提升脏检查执行速度 尽量减少页面 ...
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
To demonstrate the difference between mutability and immutability, imagine taking a drink from a gla ...
- [Angular 2] implements OnInit, OnDestory for fetching data from server
Link: https://angular.io/docs/js/latest/api/core/OnInit-interface.html, https://www.youtube.com/watc ...
- 使用 Immutable Subject 来驱动 Angular 应用
现状 最近在重构手上的一个 Angular 项目,之前是用的自己写的一个仿 Elm 架构的库来进行的状态管理,期间遇到了这些痛点: 样板代码太多 异步处理太过繁琐 需要单独维护一个 npm 包 其中, ...
- [Immutable.js] Transforming Immutable Data with Reduce
Immutable.js iterables offer the reduce() method, a powerful and often misunderstood functional oper ...
- [Immutable.js] Using fromJS() to Convert Plain JavaScript Objects into Immutable Data
Immutable.js offers the fromJS() method to build immutable structures from objects and array. Object ...
随机推荐
- 各版本IIS安装方法
各版本IIS安装方法 Windows 2000 V5.0 将操作系统安装光盘放入光驱,打开“控制面板”→“添加或删除程序”→“添加/删除 Windows 组件”,勾选“Internet信息服务(I ...
- hibernate注解原理
持续更新中.. hibernate注解用的是java注解,用到的是java反射机制.
- hadoop2.4.1伪分布式搭建
1.准备Linux环境 1.0点击VMware快捷方式,右键打开文件所在位置 -> 双击vmnetcfg.exe -> VMnet1 host-only ->修改subnet ip ...
- mysql innerjoin left join right join 解析
毕业半年多时间,一直都没有学习好join 之前一直是先从一个表里面取出数据然后,然后再从另外一个表里面取出数据,然后再写一个函数循环格式化数据. 还是先写一下学到的东西吧! 转载自w3school ...
- static变量的使用
静态变量 类型说明符是static. 静态变量属于静态存储方式,其存储空间为内存中的静态数据区(在静态存储区内分配存储单元),该区域中的数据在整个程序的运行期间一直占用这些存储空间(在程序整个运行期间 ...
- NET Core & VS Code 之路(2) Web API
NET Core & VS Code 之路(2) Web API 开发Core项目的条件 Visual Studio 2015 Update 3 .NET Core 1.0.0 - VS 20 ...
- Win10使用小技巧
Win10技巧4.命令行支持Ctrl+V 这项功能使用的人不多,但绝对是跨时代的,因为你终于可以放心大胆地在命令提示符里使用Ctrl+V来粘贴剪贴板内容了.而在此之前,Ctrl+V换来的结果只是一个^ ...
- Swift—do-try-catch错误处理模式-备
Swift 1.x的错误处理模式存在很多弊端,例如:为了在编程时候省事,给error参数传递一个nil,或者方法调用完成后不去判断error是否为nil,不进行错误处理. let contents = ...
- Top 100 English Verbs
accept allow ask believe borrow break bring buy can/be able cancel change clean comb complain cough ...
- [JS]_proto_和prototype到底有啥区别
是时候拿出我珍藏多年的这张图了: 首先,要明确几个点: 1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的 ...