[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 ...
随机推荐
- python3 继承与组合
什么叫继承? 所谓继承,就是class_A里面的功能从class_B中直接获取,从而节约了代码且使用方便. 什么叫组合? 除了继承,还有一种我们可以实现目的的方式,那就是组合,同样可以节约代码.只不过 ...
- jquery 将函数作为参数传递
等待执行函数 这是自己封装的layui的询问框 //------[询问框](parameter:执行函数的参数,f_name:执行函数,msg:提示,time:自动消失时间,btn_arr:按钮组,o ...
- python--(常用模块-2序列化)
python--(常用模块-2序列化) 一.序列化: 把对象打散成bytes或者字符串. 方便存储和传输 序列化 把bytes或者字符串转换回对象. 反序列化 # dumps 序列化. 把对象转化成b ...
- oracle数据库服务介绍
共有7个服务,这七个服务的含义分别为: 1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写入服务,VSS(Volume Shadow Copy Service ...
- ASP.NET-js和C#混合编程的例子
使用<text>这个伪元素来强制Razor从编译模式返回到内容模式: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
- Android自己定义百度地图缩放图标
自己定义实现Android百度地图的缩放图标,须要自己定义一个缩放控件,实现效果例如以下: 这里的缩放效果,实现了点击button能够对地图的放大缩小,通过手势放大与缩小也控制缩放图标的可用状态.详细 ...
- HDU 4308 Contest 1
纯BFS+优先队列扩展. #include <iostream> #include <cstdio> #include <cstring> #include < ...
- Chisel实验笔记(四)
在<Chisel实验笔记(二)>中.通过编写TestBench文件,然后使用Icarus Verilog.GtkWave能够測试,查看相关波形.比較直观,在<Chisel实验笔记(三 ...
- vuex3
以下是一个表示“单向数据流”理念的极简示意: 但是,当我们的应用遇到多个组件共享状态时,单向数据流的简洁性很容易被破坏: 多个视图依赖于同一状态. 来自不同视图的行为需要变更同一状态. 这就是 Vue ...
- hexo创建的tags和categories页面为空的解决办法
title: hexo创建的tags和categories页面为空的解决办法 toc: false date: 2018-04-16 02:26:10 主题:landscape 添加type以及men ...