[MST] Attach Behavior to mobx-state-tree Models Using Actions
2018-01-23 20:18
323 查看
Models are not just a nifty feature for type checking. They enable you to attach behavior to your actions in a straightforward and highly discoverable way
In this lesson, you will learn:
How to define actions on models
How to avoid
Models can only be modified using actions, and are further read-only from the outside
Action is the only way to update model, model is designed to be am immutable object. Even you use 'push', 'splice' in the actions, it won't affect.
Test:
in the test, we do 'WishListIem.create':
Since we already defined that each item should be a WishListItem in the model, therefore, we can skip 'WishListIem.create':
In this lesson, you will learn:
How to define actions on models
How to avoid
thisissues by using self
Models can only be modified using actions, and are further read-only from the outside
Action is the only way to update model, model is designed to be am immutable object. Even you use 'push', 'splice' in the actions, it won't affect.
import { types } from "mobx-state-tree" export const WishListItem = types .model({ name: types.string, price: types.number, image: "" }) .actions(self => ({ changeName(newName) { self.name = newName }, changePrice(newPrice) { self.price = newPrice }, changeImage(newImage) { self.image = newImage } })) export const WishList = types .model({ items: types.optional(types.array(WishListItem), []) }) .actions(self => ({ add(item) { self.items.push(item) } }))
Test:
import { getSnapshot, onSnapshot, onPatch } from "mobx-state-tree" 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("") item.changeName("Narnia") expect(item.name).toBe("Narnia") }) 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) }) it("can add new items", () => { const list = WishList.create() list.add( WishListItem.create({ name: "Chesterton", price: 10 }) ) expect(list.items.length).toBe(1) expect(list.items[0].name).toBe("Chesterton") list.items[0].changeName("Book of G.K. Chesterton") expect(list.items[0].name).toBe("Book of G.K. Chesterton") })
in the test, we do 'WishListIem.create':
it("can add new items", () => { const list = WishList.create() list.add( WishListItem.create({ name: "Chesterton", price: 10 }) ) expect(list.items.length).toBe(1) expect(list.items[0].name).toBe("Chesterton") list.items[0].changeName("Book of G.K. Chesterton") expect(list.items[0].name).toBe("Book of G.K. Chesterton") })
Since we already defined that each item should be a WishListItem in the model, therefore, we can skip 'WishListIem.create':
list.add({ name: "Chesterton", price: 10 })
相关文章推荐
- [MST] Describe Your Application Domain Using mobx-state-tree(MST) Models
- [MST] Test mobx-state-tree Models by Recording Snapshots or Patches
- [MST] Create an Entry Form to Add Models to the State Tree
- [Mobx] Use MobX actions to change and guard state
- [Mobx] Using mobx to isolate a React component state
- [MST] Restore the Model Tree State using Hot Module Reloading when Model Definitions Change
- [MST] Build Forms with React to Edit mobx-state-tree Models
- Failed to load viewstate.The control tree into which viewstate is being loaded must match the control tree that......
- 知识图谱4-【再看一篇论文《End-to-End Relation Extraction using LSTMs on Sequences and Tree Structures》】
- Using GoldenGate EVENTACTIONS to customize processing
- Using Recursive Common table expressions to represent Tree structures
- Optimal Portfolio Modeling, CD-ROM includes Models Using Excel and R: Models to Maximize Returns and
- How to show call tree using GNU cflow
- [MST] Derive Information from Models Using Views
- Java Secret: Using an enum to build a State machine(Java秘术:用枚举构建一个状态机)
- Using SetAdded and SetModified to Change RowState
- php 5.3:connection failed:SQLSTATE[HY000] [2000] mysqlnd cannot connect to MySQL 4.1+ using old auth
- [React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer
- [译]From User Input to Animations Using State Machines
- Using the itemDoubleClick event to open nodes in a Flex Tree control