博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[MST] Describe Your Application Domain Using mobx-state-tree(MST) Models
阅读量:7078 次
发布时间:2019-06-28

本文共 1426 字,大约阅读时间需要 4 分钟。

In this lesson, we introduce the running example of this course, a wishlist app. We will take a look at the core of mobx-state-tree (MST), models. Models describe the shape of your state and perform type validation.

You will learn:

  • Defining models using types.Model
  • Instantiating models from JSON using Model.create
  • Primitive types: types.string & types.number
  • Type inference for primitive types
  • types.array
  • types.optional
  • Composing models into a model tree
  • Testing models using jest

 

To create a model:

import { types } from "mobx-state-tree"export const WishListItem = types.model({    name: types.string,    price: types.number,    image: ""})export const WishList = types.model({    items: types.optional(types.array(WishListItem), [])})

'types' is similar to React PropTypes.

 

Once model is created, then we can write tests to verify:

import { WishList, WishListItem } from "./WishList"it("can create a instance of a model", () => {    const item = WishListItem.create({        name: "Chronicles of Narnia Box Set - C.S. Lewis",        price: 28.73    })    expect(item.price).toBe(28.73)    expect(item.image).toBe("")})it("can create a wishlist", () => {    const list = WishList.create({        items: [            {                name: "Chronicles of Narnia Box Set - C.S. Lewis",                price: 28.73            }        ]    })    expect(list.items.length).toBe(1)    expect(list.items[0].price).toBe(28.73)})

 

转载地址:http://mopml.baihongyu.com/

你可能感兴趣的文章
Banner框架
查看>>
php soap实例讲解
查看>>
操作文件夹,删除所有空文件夹
查看>>
Yii1.1应用升级到Yii2.0的一些注意点
查看>>
dede使用方法----调用导航
查看>>
最低位 【杭电-HDOJ-1196】 附题
查看>>
结构化方法、面向对象方法的区别
查看>>
算法笔记_192:历届试题 买不到的数目(Java)
查看>>
WebViewJavascriptBridge-Obj-C和JavaScript互通消息的桥梁
查看>>
linux启动基本流程
查看>>
VB.NET+三层 机房收费系统之组合查询
查看>>
MetaSploit攻击实例讲解------工具Meterpreter常用功能介绍(kali linux 2016.2(rolling))(详细)...
查看>>
JavaScript面向对象之类的创建
查看>>
IPsec ISAKMP(转)
查看>>
jquery js解析函数、函数直接调用
查看>>
对于eclipse building workspaces 慢的问题,解决方法
查看>>
ORACLE-行转列
查看>>
Java线程池关闭1-shutdown和isTerminated<转>
查看>>
【论文:麦克风阵列增强】An Algorithm For Linearly Constrained Adaptive Array Processing
查看>>
JPA相关注解
查看>>