[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] lodash: memoize() to improve the profermence
Link: https://lodash.com/docs#memoize Example: .service('UserPresenter', function(UserConstants){ va ...
- [Redux] Passing the Store Down Explicitly via Props
n the previous lessons, we used this tool to up level variable to refer to the Redux chore. The comp ...
- qwtplot3D安装及运行-----终结解决方案
..\qwtplot3d\include\qwt3d_openglhelper.h:67: 错误:'gluErrorString' was not declared in this scope..\q ...
- (转).net开发者对android第二周的学习体会
这一周相对没有春节时这么闲了,白天也比较多的工作要做,每天晚上又要被我三岁的女儿折腾到十点, 实在没有多少时间学习.在前一周的基础上,这周我试着自己练习写了一个个人管理的android的程序,主要实现 ...
- 表格table常见的边框设置和初步的立体效果
做网页时经常会遇到表格,常见的表格如下: <style type="text/css"> .tbtest0 { width:500px; height:200px; b ...
- HTML与JS
网页显示过程中的处理流程: 分析HTML 构造DOM树 载入外部JS文件及CSS文件 载入图像文件等外部资源 JS在分析后开始运行 全部完成 JS的表述方式及其执行流程: <script> ...
- win32系统信息获取
#include <Windows.h> #include "resource.h" #include <strsafe.h> PTSTR BigNumTo ...
- (原)ubuntu安装libtbb.so.2
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6098132.html 参考网址: https://launchpad.net/ubuntu/+sour ...
- beini系列_2_beini装入虚拟机
- 阿里云Centos7使用yum安装MySQL5.6的正确姿势
阿里云Centos7使用yum安装MySQL5.6 阿里云Centos7使用yum安装MySQL5.6 前言:由于某些不可抗力,我要在自己的阿里云服务器上搭建hadoop+hive+mysql+tom ...