[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 ...
随机推荐
- java实验程序基础中的问题总结 java图形化界面
一,课程中的问题 1,知道程序实现的大体框架,但是不能具体到每一个细节,这需要平时的积累. 2,在不同文件夹中定义的类之间有没有联系,类与类之间可以通过接口相互联系. 3,如何在一个对话框中显示文本, ...
- python中try…except的使用,处理程序异常
通常情况下,在python中运行程序,多多少少会出现程序异常的问题,try……except能很好的解决程序中的异常.以下是其用法,在不同位置时进行什么样的工作和起到什么样的作用. try: 可能出现异 ...
- pytorch 6 batch_train 批训练
import torch import torch.utils.data as Data torch.manual_seed(1) # reproducible # BATCH_SIZE = 5 BA ...
- css预编译器——Less的使用
方法一:仅介绍在客户端环境下使用的方法 1 新建test.less并引入.less该文件(和css一样在head处引入),注意rel="stylesheet/less": &l ...
- 【codeforces 738E】Subordinates
[题目链接]:http://codeforces.com/problemset/problem/738/E [题意] 给你一个类似树形的关系; 然后告诉你某个人头顶上有多少个上司numi; 只有fat ...
- war包放入tomcat
1.找到tomcat的安装路径(如:D:\example\download\set\apache-tomcat-7.0.23) 2.D:\example\download\set\apache-tom ...
- ios学习--第三方框架-MBProgressHUD以及扩展
MBProgressHUD提示框官网地址:https://github.com/jdg/MBProgressHUD 一. 模式 首先, MBProgressHUD有以下几种视图模式. typedef ...
- php设计模式之工厂方法模式
php设计模式之工厂方法模式 工厂方法模式 工厂方法模式(Factory Method Pattern)又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式或者多态工厂(Pol ...
- hdoj--3666--THE MATRIX PROBLEM(差分约束+SPFA深搜)
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- nyoj--1239--引水工程(最小生成树)
引水工程 时间限制:2000 ms | 内存限制:65535 KB 难度: 描述 南水北调工程是优化水资源配置.促进区域协调发展的基础性工程,是新中国成立以来投资额最大.涉及面最广的战略性工程,事 ...