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. ansible shell模块

    [root@ftp:/root] > ansible ansible01 -u root -k -m shell -a 'hostname' SSH password: ansible01 | ...

  2. PHP算法之四大基础算法

    前言 虽然工作中,你觉得自己并没有涉及到算法这方面的东西,但是算法是程序的核心,一个程序的好与差,关键是这个程序算法的优劣,所以对于冒泡排序.插入排序.选择排序.快速排序这四种基本算法,我想还是要掌握 ...

  3. 【hdu 6319】Ascending Rating

    [链接] 我是链接,点我呀:) [题意] 给你一个长为n的数组a 让你对于每个长度为m的窗口. 算出其中的最大值以及从左往右遍历的时候这个最大值更新的次数. [题解] 单调队列. 从后往前滑动窗口. ...

  4. Reentrant protected mode kernel using virtual 8086 mode interrupt service routines

    A method for allowing a protected mode kernel to service, in virtual 8086 mode, hardware interrupts ...

  5. poj3061 Subsequence ,尺取法

    A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...

  6. Systemd启动图形界面过程

    1 启动命令 systemctl isolate graphical.target 2 启动过程: 文件:/etc/systemd/system/graphical.target 来自:systemd ...

  7. bzoj2663: [Beijing wc2012]灵魂宝石(二分+匈牙利)

    2663: [Beijing wc2012]灵魂宝石 题目:传送门 题解: 又是一道卡精度的题目. 很容易就可以看出单调性啊,如果R越大,选的人就越多,R越小,选的人就越少. 那最小值就直接搞咯. 那 ...

  8. CoreData 从入门到精通(四)并发操作

    通常情况下,CoreData 的增删改查操作都在主线程上执行,那么对数据库的操作就会影响到 UI 操作,这在操作的数据量比较小的时候,执行的速度很快,我们也不会察觉到对 UI 的影响,但是当数据量特别 ...

  9. nyoj--55--懒省事的小明(STL优先队列)

    懒省事的小明 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述       小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种类分 ...

  10. NOIP 2012 T2 国王游戏 (贪心+高精)

    思路: 呃呃网上那么多题解写得都不错-.. 就是高精 巨坑... 这里展出的是任氏高精(纯自己yy滴) //By SiriusRen #include <cstdio> #include ...