[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 ...
随机推荐
- [Javascript] How to use JavaScript's String.replace
In JavaScript, you can change the content of a string using the replace method. This method signatur ...
- PHP中$_SERVER的具体參数与说明
PHP编程中常常须要用到一些server的一些资料.特把$_SERVER的具体參数整理下,方便以后使用. $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名称,与 document ...
- 使用Express创建一个简单的示例
1.安装Express 使用npm包安装工具来安装Express安装包,打开npm命令行,输入: npm install -g express 2.创建一个工程 本示例是在windows下创建的,项目 ...
- 使用Dataset
string sqlStr="Select * from Tb_news"; SqlDataAdapter myDa=new SqlDataAdapter(SqlStr,myCon ...
- pfile,spfile 初始化参数文件顺序【weber出品】
一.初始化参数文件 启动实例时会读取初始化参数文件.参数文件有两种类型: 1.服务器参数文件:这是首选类型的初始化参数文件.这是一个由数据库服务器写入或读取的二进制文件,不得手动进行编辑.此文件驻留在 ...
- MD5 Tool 工具类
package com.example.secret.tools; import java.io.UnsupportedEncodingException; import java.security. ...
- hdu 1215 七夕节
Problem Description 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!" ...
- html label 标签的 for 属性
如果您在 label 元素内点击文本,就会触发此控件.就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上. 有两种使用方法: 方法1 使用for属性 <label for ...
- MFC 多线程
MFC对多线程编程的支持 MFC中有两类线程,分别称之为工作者线程和用户界面线程.二者的主要区别在于工作者线程没有消息循环,而用户界面线程有自己的消息 队列和消息循环. 工作者线程没有消息机制,通 ...
- Labeling Balls--poj3687
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12273 Accepted: 3516 D ...