[MST] Create Dynamic Types and use Type Composition to Extract Common Functionality
Since MST offers a runtime type system, it can create and compose types on the fly, making it possible to reuse logic in new and powerful ways.
In this lesson you will learn:
- That MST types are immutable and composed together behind the scenes
- How to compose types explicitly by using
types.compose
- How to create dynamic, parameterized types by leveraging that MST types are first class javascript citizens
- import { types, flow, getSnapshot, onSnapshot } from "mobx-state-tree"
- export function createStorable(collection, attribute) {
- return types.model({}).actions(self => ({
- save: flow(function* save() {
- try {
- yield window.fetch(`http://localhost:3001/${collection}/${self[attribute]}`, {
- method: "PUT",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify(getSnapshot(self))
- })
- } catch (e) {
- console.error("Uh oh, failed to save: ", e)
- }
- }),
- afterCreate() {
- onSnapshot(self, self.save)
- }
- }))
- }
- import { types, flow, getParent, applySnapshot, getSnapshot, onSnapshot } from "mobx-state-tree"
- import { WishList } from "./WishList"
- import { createStorable } from "./Storable"
- const User = types.compose(
- types
- .model({
- id: types.identifier(),
- name: types.string,
- gender: types.enumeration("gender", ["m", "f"]),
- wishList: types.optional(WishList, {}),
- recipient: types.maybe(types.reference(types.late(() => User)))
- })
- .actions(self => ({
- getSuggestions: flow(function* getSuggestions() {
- const response = yield window.fetch(
- `http://localhost:3001/suggestions_${self.gender}`
- )
- self.wishList.items.push(...(yield response.json()))
- })
- })),
- createStorable("users", "id")
- )
[MST] Create Dynamic Types and use Type Composition to Extract Common Functionality的更多相关文章
- Unable to create a constant value of type 'Closure type'
使用Linq to Entities的时候发生如下异常: Unable to create a constant value of type 'Closure type'. Only primitiv ...
- Create Dynamic Modal Dialog Form in AdminLTE Bootstrap template
原文地址 Create modal dialog form in jquery using bootstrap framework, slightly different from the usual ...
- unity c# script error CS0664: Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type
例如在unity c# script中定义 private float x=0.0; 则会报 error CS0664: Literal of type double cannot be implic ...
- dubbo rest返回值异常Incompatible types: declared root type
2018-08-28 17:26:02,208 [http-bio-9090-exec-1][][][][][] ERROR com.wjs.member.plugin.intercepter.Ser ...
- mybatis报错:A query was run and no Result Maps were found for the Mapped Statement、、Property [login_ip] not found on type [com.thinkgem.jeesite.common.permission.entity.PremissUser]问题解决
今天在做ssm项目的时候出现了: 先是出现 了错误: mybatis报错:A query was run and no Result Maps were found for the Mapped St ...
- Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.
代码如下: var query = from s in db.LoginUserServices join ss in db.Services on s.ServiceType equals ss.C ...
- [Angular] Using ngTemplateOutlet to create dynamic template
I can use <tamplete> syntax and a entry component as a container to create a dynamic component ...
- [MST] Create an Entry Form to Add Models to the State Tree
It is time to add new entries to the wishlist. We will achieve this by reusing forms and models we'v ...
- [Angular] Create dynamic content with <tempalte>
To create a dynamic template, we need to a entry component as a placeholder. Then we can use entry c ...
随机推荐
- layui select change
<select lay-filter="test"></select> layui.use([ 'form'], function() { var form ...
- 洛谷P1138 第k小整数
我偏不用sort Treap好题啊 看到只有一个人写Treap,而且写的不清楚,那我就来详细地写一下,方便新人学习 第(-1)部分:前置知识 二叉查找树:满足左子树的数据都比根节点小,右子树的数据都比 ...
- 关于一些运算(&(与运算)、|(或运算)、^(异或运算)........)的本质理解【转】
看到一篇博客,关于一些运算的解析,觉得有用,怕以后找不着,直接复制下来,以备以后学习用 原文链接:https://blog.csdn.net/xiaopihaierletian/article/det ...
- Mysql 5.7 官方文档翻译
始于 2017年4月1日-愚人节 1.1 MySQL 5.7 新功能 本章节介绍了MySQL 5.7 新版本中新增.废弃.删除的功能. 在1.5章节 Section 1.5, "Server ...
- mysql5.7官网直译SQL语句优化--select语句优化
8.2 sql语句优化 大致内容如下: 8.2.1:SELECT语句的优化 8.2.2:优化子查询,派生表和试图引用 8.2.3:优化INFORMATION_SCHEMA查询 8.2.4:优化数据改变 ...
- 我是怎么从项目中的lib加JAR更换为maven管理的
原来我对maven的使用应该还是去年的时候吧,当时对maven并不感冒(请不要吐槽哈),认为为什么一定要用maven来管理呢,我自己管理jar不是一样么,当时还认为自己管理jar还各种方便还对mave ...
- bzoj4868: [Shoi2017]期末考试(三分法)
4868: [Shoi2017]期末考试 题目:传送门 题解: Get到一个新姿势...三分法 一开始百度百科的时候下了一跳...中国...的根??? 学懂了之后其实运用起来就根二分差不多啊,不过证明 ...
- zzuoj--10399--Turing equation(模拟)
Turing equation Time Limit: 1 Sec Memory Limit: 128 MB Submit: 152 Solved: 85 [Submit][Status][Web ...
- 极客时间 Mysql实战45讲 07讲行锁功过:怎么减少行锁对性能的影响笔记 极客时间
极客时间 Mysql实战45讲 07讲行锁功过:怎么减少行锁对性能的影响笔记 极客时间极客时间 Mysql实战45讲 07讲行锁功过:怎么减少行锁对性能的影响笔记 极客时间 笔记体会: 方案一,事务相 ...
- C# 对象初始化器和集合初始化器
/// <summary>/// 图书类/// </summary>public class Book { /// <summary> /// 图书 ...