[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 ...
随机推荐
- CSS变量实用指南及注意事项
近年来,一些动态特性已经开始成为 CSS 语言本身的一部分. CSS变量 – 官方的术语为 "自定义属性" – 已经已经加入规范并且具有很好的浏览器支持,而 CSS mixins ...
- 原生JS封装ajax以及request
一.封装原生的xhr为ajax类 xhr以及用法见之前的文章 1.根据url确定请求的头部以及别的信息. var _headerConfig = {}; if(url.indexOf('getcapt ...
- bat启动.exe的应用程序
新建一个文本文档,编写如下,完成后保存将后缀名txt改为bat即可. rem 启动***(要启动的服务名) @echo off rem 程序安装的顶层目录 d: rem 设置显示文字颜色 color ...
- invalid application of `sizeof' to incomplete type `char[] '
在写代码时,我想用extern来关联一个数组,然后利用sizeof计算数组的大小,代码如下: ... extern char a[]; #define b size=(sizeof(a)/sizeof ...
- mysql存储小数
线下不知道什么版本的古董了,经本人亲测,varchar类型的数据,可以直接执行mysql的sum函数. ________________________________________________ ...
- Java基础学习总结(48)——Java 文档注释
Java只是三种注释方式.前两种分别是// 和/* */,第三种被称作说明注释,它以/** 开始,以 */结束. 说明注释允许你在程序中嵌入关于程序的信息.你可以使用javadoc工具软件来生成信息, ...
- LaTeX 表格指定宽度并居中
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50532269 在绘制表格的时候,对于特 ...
- 【MFC设置静态文本框背景为透明】
视图类中加入OnCtlColor()函数: IDC_STATIC1为静态文本框ID HBRUSH CAngleView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT n ...
- ant打包和jar包混淆
Ant是一种基于Java的build工具.相似于c语言中的makefile,这里做一记录.方便后面查看. <?xml version="1.0" encoding=" ...
- Testbench代码设计技巧
Testbench代码设计技巧 " There are many ways " to code a test case, it all depens on the creativi ...