Redundant data or caching data is a constant source of bugs. MST adheres to the philosophy that no data that can be derived should ever get stored. In this lesson, you will learn how to introduce views that declaratively derive data and which cache data smartly.

In this lesson you will learn:

  • How to introduce views on models
  • That computed properties are powered by Mobx computed fields
  • How to combine MST with Mobx utilities like reaction

To learn more about MobX basics check out my course, Manage Complex State in React Apps with MobX

Inside 'views', you can do calculation based on the model:

export const WishList = types
.model({
items: types.optional(types.array(WishListItem), [])
})
.actions(self => ({
addItem(newItem) {
// Model is immutable object, it is ok to do push here.
self.items.push(newItem);
}
}))
.views(self => ({
get totalPrice() {
return self.items.reduce((acc, curr) => curr.price + acc, 0)
}
}));
import {WishList, WishListItem} from "./WishList"
import {reaction} from 'mobx'
it('should calculate the total price of a wishlist', function () {
let changes = 0;
const list = WishList.create();
reaction(() => list.totalPrice, () => changes++)
list.addItem({
name: 'Amazon',
price: 30
});
expect(list.totalPrice).toEqual(30);
expect(changes).toEqual(1)
list.addItem({
name: 'DJ',
price: 12
});
expect(list.totalPrice).toEqual(42);
expect(changes).toEqual(2) list.items[0].changePrice(28);
expect(list.totalPrice).toEqual(40);
expect(changes).toEqual(3) list.items[0].changeName("Amazon cn");
expect(changes).toEqual(3)
});

[MST] Derive Information from Models Using Views的更多相关文章

  1. [Backbone]5. Model & View, toggle between Models and Views -- 2

    Dr. Goodparts is pretty flaky and has been cancelling a lot of appointments lately. He's asked for a ...

  2. IRGAN:A Minimax Game for Unifying Generative and Discriminative Information Retrieval Models

    https://arxiv.org/pdf/1705.10513.pdf 论文阅读笔记: https://www.cnblogs.com/liaohuiqiang/p/9694277.html htt ...

  3. Django:URL,Views,Template,Models

    准备工作:熟悉Django命令行工具 django-admin.py 是Django的一个用于管理任务的命令行工具,常用的命令整理如下: <1> 创建一个django工程 : django ...

  4. How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC

    Snesh Prajapati, 8 Dec 2014 http://www.codeproject.com/Articles/717941/How-to-Choose-the-Best-Way-to ...

  5. Alert Views

    Alert views display a concise and informative alert message to the user. Alert views convey importan ...

  6. 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)

    发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...

  7. [Windows Azure] Monitoring SQL Database Using Dynamic Management Views

    Monitoring Windows Azure SQL Database Using Dynamic Management Views 5 out of 7 rated this helpful - ...

  8. Django笔记&教程 7-1 基于类的视图(Class-based views)介绍

    Django 自学笔记兼学习教程第7章第1节--基于类的视图(Class-based views)介绍 点击查看教程总目录 1 介绍 Class-based views (CBVs) are view ...

  9. A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python)

    A Complete Tutorial on Tree Based Modeling from Scratch (in R & Python) MACHINE LEARNING PYTHON  ...

随机推荐

  1. CSS变量实用指南及注意事项

    近年来,一些动态特性已经开始成为 CSS 语言本身的一部分. CSS变量 – 官方的术语为 "自定义属性" – 已经已经加入规范并且具有很好的浏览器支持,而 CSS mixins ...

  2. 2019-03-19 SQL Server简单存储过程的创建 删除 执行

    --创建名为 Get 的有输入参数的存储过程 create proc Get --设置默认值 @TrustId int ='001' as begin select * from [DealStruc ...

  3. JavaScript 实现留言框

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. Vue 做项目经验

    Vue 做项目经验 首先需要知道最基本的东西是: Vue 项目打包:npm run build Vue生成在网页上看的端口:npm run dev 修改端口号的地方在: config文件夹下index ...

  5. js 学习思维导图

  6. dubbo标签

    <dubbo:service/> 服务配置,用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心. <dubbo:reference/&g ...

  7. 从字节码指令看重写在JVM中的实现

    Java是解释执行的.包含动态链接的特性.都给解析或执行期间提供了非常多灵活扩展的空间.面向对象语言的继承.封装和多态的特性,在JVM中是怎样进行编译.解析,以及通过字节码指令怎样确定方法调用的版本号 ...

  8. 深入分析Java中的I/O类的特征及适用场合

    Java中有40多个与输入输出有关的类.假设不理清它们之间的关系.就不能灵活地运用它们. 假设从流的流向来分,可分为输入流和输出流,而输入流和输出流又都可分为字节流和字符流.因而可将Java中的I/O ...

  9. cocos2d-x 2.2.2 在lua中更换CCSprite的图片

    废话不多说,直接上代码 --lua --获取场景 local scene= CCDirector:sharedDirector():getRunningScene() --创建精灵 local tes ...

  10. lscript.ld 链接器脚本

    sumary选项卡 lscript.ld是这个应用程序的链接器脚本. 这是实用的作为一个报告 看看内存是针相应用程序. 它也能够被编辑以改变应用程序的位置.双击Hello_Zynqàsrcà lscr ...