[Immutable,js] Immutable.Record() as data models
The Immutable.js Record() allows you to model your immutable data much like you would model data with native Javascript classes or objects. It differs from native classes because it cannot be mutated after it's creation and it always has a default value. It's an excellent construct in which to piece together your stores, be them Flux or some other storage implementation. Let's quickly create an Immutable Record().
Create a Record class:
let TodoRecord = Immutable.Record({
id: (+new Date() + Math.floor(Math.random() * 999999)).toString(36),
title: "Default Title",
items: Immutable.List(),
completed: false
});
Inside the Recode class, you can define the default value. But you cannot add any method!
New a instance:
// Create a new instance
let imTodo = new TodoRecord({
title: "New Title",
items: Immutable.List(),
completed: false
});
Update and get an value:
// Update the title
imTodo = imTodo.updateIn(['title'], val => "Immutable");
console.log(imTodo.get('title'));
Read the value:
console.log(imTodo.title); let items = imTodo.items;
let newItem = "New Item";
let updatedItems = imTodo.push(newItem); let newTodo = imTodo.set("items", updatedItems );
As you can see, the advantage by using Record instread of Map is that we can access the value directly by using:
imTodo.title;
instead of:
imTodo.get("title") //Of course this also works
[Immutable,js] Immutable.Record() as data models的更多相关文章
- Redux进阶(Immutable.js)
更好的阅读体验 更好的阅度体验 Immutable.js Immutable的优势 1. 保证不可变(每次通过Immutable.js操作的对象都会返回一个新的对象) 2. 丰富的API 3. 性能好 ...
- [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 ...
- [Javascript] Manage Application State with Immutable.js
Learn how Immutable.js data structures are different from native iterable Javascript data types and ...
- 大话immutable.js
为啥要用immutable.js呢.毫不夸张的说.有了immutable.js(当然也有其他实现库)..才能将react的性能发挥到极致!要是各位看官用过一段时间的react,而没有用immutabl ...
- React+Immutable.js的心路历程
这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...
- [Javascript] Creating an Immutable Object Graph with Immutable.js Map()
Learn how to create an Immutable.Map() through plain Javascript object construction and also via arr ...
- [Immutable.js] Exploring Sequences and Range() in Immutable.js
Understanding Immutable.js's Map() and List() structures will likely take you as far as you want to ...
- [Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types
Immutable.js provides several conversion methods to migrate one structure to another. Each Immutable ...
随机推荐
- 基础总结篇之中的一个:Activity生命周期
子曰:溫故而知新,能够為師矣.<論語> 学习技术也一样,对于技术文档或者经典的技术书籍来说,指望看一遍就全然掌握,那基本不大可能,所以我们须要常常回过头再细致研读几遍,以领悟到作者的思想精 ...
- C++11下的线程池以及灵活的functional + bind + lamda
利用boost的thread实现一个线程类,维护一个任务队列,以便可以承载非常灵活的调用.这个线程类可以方便的为后面的线程池打好基础.线程池还是动态均衡,没有什么别的.由于minGW 4.7 对 C+ ...
- EasyInvoice 使用教程 - (1) 认识 EI
原视频下载地址:EI 主界面介绍 1. 主界面截图 2. 基础资料界面截图 3. 管理员 界面截图
- Less 教程
1. 关于 less sass 的预编译处理器 LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承, 运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, ...
- Internetmap.apk实现原理分析
1.本地实现调用 程序根据data文件目录下的asinfo.json文件(包含自治域网络名和对应的坐标值),调用so文件绘制asn结点图(ASN,AutoSystemNode,自治域结点) 2.路由查 ...
- SQL Server常用脚本
一.迁移登录用户脚本: select 'create login [' + p.name + '] ' + case when p.type in('U','G') then 'from window ...
- AVD启动不了 ANDROID_SDK_HOME is defined but could not find *.ini
报错提示______________________________________________________________________ Starting emulator for AVD ...
- Masters of Doom
http://blog.codinghorror.com/you-dont-need-millions-of-dollars/ "In the information age, the ba ...
- Windows命令行(DOS命令)教程–2 (转载) http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_1.html
二.符号约定 为了便于说明格式,这里我们使用了一些符号约定,它们是通用的: C: 盘符 Path 路径 Filename 文件名 .ext 扩展名 Filespec 文件标识符 [ ] 方括号中的项目 ...
- win7添加usb3.0驱动(错误代码1392,文件或目录损坏且无法读取)
Win7添加usb3.0驱动 之前一直按照网上的方法执行dism命令挂载时,总是失败,错误代码1392,显示原因是文件或目录损坏且无法读取.这个错误以前在装机时老是出现导致系统安装不成功,在BIOS中 ...