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的更多相关文章

  1. 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 ...

  2. Create Dynamic Modal Dialog Form in AdminLTE Bootstrap template

    原文地址 Create modal dialog form in jquery using bootstrap framework, slightly different from the usual ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [Angular] Using ngTemplateOutlet to create dynamic template

    I can use <tamplete> syntax and a entry component as a container to create a dynamic component ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 关于libnmap 的一些应用

    随笔描述 nmap 可以进行端口的扫描,在安全或运维中可以说是一款不错的神奇吧,在大部分LINUX 里面都自带了nmap 这款工具,他不仅仅是端口扫描,自身还提供许多插件可以使用. 官方文档 nmap ...

  2. JAVA多线程知识总结(二)

    本文是承接上一篇文章:JAVA多线程知识总结(一) 四.Java多线程的阻塞状态与线程控制  上文已经提到线程阻塞的集中具体类型.下面主要看引起JAVA线程阻塞的方法 1,join()-----让一个 ...

  3. [luogu] P3210 [HNOI2010]取石头游戏(贪心)

    P3210 [HNOI2010]取石头游戏 题目描述 A 公司正在举办一个智力双人游戏比赛----取石子游戏,游戏的获胜者将会获得 A 公司提供的丰厚奖金,因此吸引了来自全国各地的许多聪明的选手前来参 ...

  4. OO第一单元总结__多项式求导问题

    作业一.含幂函数的简单多项式的求导 (1)基于度量的程序结构分析 1. 统计信息图: 2. 结构信息图: 3. 复杂度分析 基本复杂度(Essential Complexity (ev(G)).模块设 ...

  5. js基础——事件绑定(事件监听)

    JavaScript事件一共有三种监听方法分别如下: 1.事件监听一夹杂在html标签内 <div id="box" onClick="alert('HELLO W ...

  6. 监控myserver计数器

  7. 楼宇自控-BA系统流程总图

    总结一下过程中的节点和技能,希望能对其他人有所帮助

  8. 洛谷 P1617 爱与愁的一千个伤心的理由

    P1617 爱与愁的一千个伤心的理由 题目背景 (本道题目隐藏了两首歌名,找找看哪~~~) <爱与愁的故事第一弹·heartache>第二章. 经历了心痛后,爱与愁大神不行了. 题目描述 ...

  9. 调用支付宝SDK问题

    近期做了一个项目里面要有支付.银联.支付宝,微信支付 我先一个一个写吧 先说支付宝SDK 支付宝SDK放进project里面之后肯定会报错.这时候你就要一个一个改掉 1. 2. 3. 哎 我懒得写了. ...

  10. True or False? and WHY??? Java HashSet Contains

    import java.util.HashSet; public class MyClass { public String s; public MyClass(String s) { this.s ...