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. Hadoop2.9.1安装教程_环境Ubuntu_VMware安装

    一,环境选择 Hadoop需要运行在linux系统之下,所以有以下两种选择:1,安装双系统,缺点:此方式比较麻烦而且并不适合初学者,因为之后的安装以及配置过程可能会遇到许多问题,这需要我们上网去搜索. ...

  2. Android开发新手HelloWorld解析

    首先看这个 HelloWorld 类. Java代码public class HelloWorld extends Activity {       /** Called when the activ ...

  3. 从CSDN搬过来

    https://blog.csdn.net/qq_34416123 从CSDN搬过来 神奇的代码竟然没有弄成博客园这里面的格式 所以以前的很多博客的代码都是直接放在那里了. 懒得去改了.

  4. 紫书 习题7-13 UVa 817(dfs+栈求表达式的值)

    题目链接  点击打开链接 这道题分为两个部分, 一用搜索枚举每种可能, 二计算表达式的值, 有挺多细节需要注意 特别注意我的代码中在计算表达式的值中用到了一个!(代码枚举中的!表示不加符号, 我现在说 ...

  5. 记一次在BroadcastReceiver或Service里弹窗的“完美”实践

    事情是这样的,目前在做一个医疗项目,需要定时在某个时间段比如午休时间和晚上让我们的App休眠,那么这个时候在休眠时间段如果用户按了电源键点亮屏幕了,我们就需要弹出一个全屏的窗口去做一个人性化的提示,“ ...

  6. C++中的字节对齐

    本博客(http://blog.csdn.net/livelylittlefish)贴出作者(三二一.小鱼)相关研究.学习内容所做的笔记,欢迎广大朋友指正! 字节对齐 1. 基本概念字节对齐:计算机存 ...

  7. 【Oracle学习笔记】

    内容主要包括: (1)三种循环及其简化 (2)游标的使用 (3)异常处理 (4)存储过程 (5)存储函数 (6)触发器 (7)其它pl/sql操作 ---------------loop循环定义变量- ...

  8. PHP第八课 字符串拆分经常使用函数

    课程概要: 通过这节课可以对字符串进行主要的操作. 字符串知识点: 1.字符串的处理介绍 2.经常使用的字符串输出函数 3.经常使用的字符串格式化函数 4.字符串比較函数 5.正則表達式在字符串中的应 ...

  9. 【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】

    [058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...

  10. POJ 1948 DP

    题意:给你n个木棍(n<=40)每个木棍长度<=40,问用上所有的木棍拼成的三角形的面积的最大值,并输出面积*100的值(不四舍五入) 如果没有解,输出-1. 思路: 背包判断可达性. f ...