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. vue 根据下拉框动态切换form的rule

    taskCategorySelect (val) { // 任务类别下拉选择 if ( val == 5 ) { this.cameraORgateway = false; // true不可以使用 ...

  2. Cygwin(类UNIX模拟环境)&CURL(强大的http命令行工具)

    前言: 需要我用curl试下能否发送post请求调起公司的仿真系统(目前) 跟着大佬的脚步,亲测一把~ 感谢大佬的提供的博客和指导 @咩神 个人博客园及来源地址 Cygwin(类UNIX模拟环境) 一 ...

  3. Golang-import-introduce

    本文主要讲解golang中import关键字的用法 import( "fmt" ) //然后在代码里面可以通过如下的方式调用 fmt.Println("hello wor ...

  4. tomcat的连接数与线程池

    在使用tomcat时,经常会遇到连接数.线程数之类的配置问题,要真正理解这些概念,必须先了解Tomcat的连接器(Connector). 在前面的文章中写到过:Connector的主要功能,是接收连接 ...

  5. C#-GC基础(待补充)

    Finalize方法与Dispose方法区别 1. Finalize只释放非托管资源: 2. Dispose释放托管和非托管资源: // D 是神的天敌3. 重复调用Finalize和Dispose是 ...

  6. [ReactVR] Add Lighting Using Light Components in React VR

    In order to illuminate a scene containing 3D objects a lighting setup is required. In this lesson we ...

  7. cocos2d-x:读取指定文件夹下的文件名称+解决中文乱码(win32下有效)

    援引:http://blog.csdn.net/zhanghefu/article/details/21284323 http://blog.csdn.net/cxf7394373/article/d ...

  8. phonegap(cordova) 自己定义插件代码篇(六)----android ,iOS 微信支付工具整合

    还是那句话,在使用插件代码篇的时候,请先了解插件机制(如整合原生插件先阅读原生插件文档.非常重要.非常重要!非常重要!),如未了解,请先阅读入门篇.这里就专贴关键代码 必须先把官方sdk 依照要求一步 ...

  9. iOS UI01_UIView

    // //  AppDelegate.m //  UI01_UIView // //  Created by dllo on 15/7/29. //  Copyright (c) 2015年 zhoz ...

  10. kentico中的urls

    alias是默认的访问页面 page aliases中可以手动指定访问一个url,然后跳转到当前的页面