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的更多相关文章

  1. Redux进阶(Immutable.js)

    更好的阅读体验 更好的阅度体验 Immutable.js Immutable的优势 1. 保证不可变(每次通过Immutable.js操作的对象都会返回一个新的对象) 2. 丰富的API 3. 性能好 ...

  2. [Immutable.js] Transforming Immutable Data with Reduce

    Immutable.js iterables offer the reduce() method, a powerful and often misunderstood functional oper ...

  3. [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 ...

  4. [Javascript] Manage Application State with Immutable.js

    Learn how Immutable.js data structures are different from native iterable Javascript data types and ...

  5. 大话immutable.js

    为啥要用immutable.js呢.毫不夸张的说.有了immutable.js(当然也有其他实现库)..才能将react的性能发挥到极致!要是各位看官用过一段时间的react,而没有用immutabl ...

  6. React+Immutable.js的心路历程

    这段时间做的项目开发中用的是React+Redux+ImmutableJs+Es6开发,总结了immutable.js的相关使用姿势: Immutable Data 顾名思义是指一旦被创造后,就不可以 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Hadoop集群运行JNI程序

    要在Hadoop集群运行上运行JNI程序,首先要在单机上调试程序直到可以正确运行JNI程序,之后移植到Hadoop集群就是水到渠成的事情. Hadoop运行程序的方式是通过jar包,所以我们需要将所有 ...

  2. C#中的一些复习。

    摘自http://www.cnblogs.com/yuchengping/p/3151537.html 等日后自己来完善. 基本概念 1..NET是平台,C#是.NET上的一门语言. 2.C#的异常处 ...

  3. avalon学习笔记一 列表及条件过滤

    好长时间都没有更新博客了,不是因为没有学习新的东西,而是到了新的单位每天玩命加班实在是太累了!经过一年的努力吧,终于可以轻松一下了.废话少说,直接干货吧! 由于是学习阶段,就直接拿了公司的二级页面做了 ...

  4. C# 标签打印示例 1

    初次写博客,有哪些不足的地方,还请多多指点,给予建议,谢谢! 如若想要源码,请留言.        本实例是在Webservice 中通过excel做模板来打印标签.具体需求是:一个订单一页纸打印4行 ...

  5. 使用.NET框架、Web service实现Android的文件上传(一)

    上面是上传结果的展示,下面具体讲一下实现过程. 一.Web Service (.NET) namespace VedioPlayerWebService.service.vedios { [WebSe ...

  6. angular 指令梳理 —— 前端校验

    angular js内置校验的扩展 校验成功则 scope.formName.$valid=true 校验失败  元素的class: ng-invalid 成功:.ng-valid /** * 校验指 ...

  7. MySql安装与卸载

    win2003下MySql的配置 准备相关组件 1.MySql安装包 mysql-installer-commercial- 5.6.14.0.msi 2.Microsoft .NETFramewor ...

  8. createjs基础

    <canvas id="gameView" width="400px" height="400px" style="back ...

  9. js 计算两个日期之间的周数

    //返回两个日期相差的周数 function WeeksBetw(date1, date2) { //这里的date1,date2都是Date对象 var d1 = new Date(date1); ...

  10. Spring中的WebDataBinder浅析

    Spring可以自动封装Bean,也就是说可以前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等).如果传递过来 ...