[MST] Derive Information from Models Using Views
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的更多相关文章
- [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 ...
- 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 ...
- Django:URL,Views,Template,Models
准备工作:熟悉Django命令行工具 django-admin.py 是Django的一个用于管理任务的命令行工具,常用的命令整理如下: <1> 创建一个django工程 : django ...
- 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 ...
- Alert Views
Alert views display a concise and informative alert message to the user. Alert views convey importan ...
- 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)
发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...
- [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 - ...
- Django笔记&教程 7-1 基于类的视图(Class-based views)介绍
Django 自学笔记兼学习教程第7章第1节--基于类的视图(Class-based views)介绍 点击查看教程总目录 1 介绍 Class-based views (CBVs) are view ...
- 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 ...
随机推荐
- vue通过路由实现页面刷新
vue 开发微信商城项目,需求如下: 购物车页面跳转到详情页,购物车页面包含了多个组件,点击结算跳转到订单页面,从订单返回时,购物车页面没有刷新,由于购物车组件之间通过bus实现事件传递,页面跳转(非 ...
- QT_圆_直线_三角t
MyImgTest.h: #ifndef MYIMGTEST_H#define MYIMGTEST_H #include <QWidget> class MyImgTest : publi ...
- [LeetCode] 242. 有效的字母异位词 valid-anagram(排序)
注意这里字母异位词的定义是:字母类别及个数都要一样,只是排列顺序不同. class Solution(object): def isAnagram(self, s, t): ""& ...
- java静态方法
静态方法(全局方法)不能访问this(当前对象)它和类没有关系,会有逻辑错误,当调用静态方法的时候不需要创建对象 可以直接为boolean result = MyTest.isPrime();直接访问 ...
- Maven系列--web.xml 配置详解
一 .web.xml介绍 启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 紧接着,容 ...
- 【codeforces 743E】Vladik and cards
[题目链接]:http://codeforces.com/problemset/problem/743/E [题意] 给你n个数字; 这些数字都是1到8范围内的整数; 然后让你从中选出一个最长的子列; ...
- 【codeforces 508E】Artur and Brackets
[题目链接]:http://codeforces.com/problemset/problem/508/E [题意] 让你构造一个括号字符串; 使得每个从左往右数第i个左括号在这个括号序列中与之匹配的 ...
- linux gnome kde点滴
2014.12.08 下面切换的方法对于fedora 17没有效果,对于fedora 17, 要使用system-switch-displaymanager,出现 点击相应的选项,然后就进入相应的启动 ...
- POJ 2773
不经意看见dis后的“mod”一词后,瞬间有了思路,点进去看,却发现别人想的和我的不一样——! 我是这样想的,利用的是剩余系+欧几里德带余除法的性质. 若两者GCD=1,则必有除数和余数GCD=1.于 ...
- UVA 436 - Arbitrage (II)(floyd)
UVA 436 - Arbitrage (II) 题目链接 题意:给定一些国家货币的汇率.问是否能通过不断换货币使钱得到增长 思路:floyd,完事后推断一下有没有连到自己能大于1的情况 代码: #i ...