In this lesson we will dive a bit more into the tree semantics of MST.

In this lesson you will learn:

  • Actions can only modify their own subtree
  • The use of getParent to find the parent of a model instance
  • Using destroy to remove an instance entirely from the tree

The whole point for removing data from model is call 'destroy', sometime you might need 'getParent' if the data you want to remove is not in current tree node.

import { types, getParent, destroy } 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
},
remove() {
getParent(self, 2).remove(self)
}
})) export const WishList = types
.model({
items: types.optional(types.array(WishListItem), [])
})
.actions(self => ({
add(item) {
self.items.push(item)
},
remove(item) {
destroy(item)
}
}))
.views(self => ({
get totalPrice() {
return self.items.reduce((sum, entry) => sum + entry.price, 0)
}
}))

[MST] Remove Model Instances from the Tree的更多相关文章

  1. Odoo 二次开发教程(三)-第一个Model及Form、Tree视图

    创建完我们的模块,接下来我们就要为我们的模块添加一些对象.今天我们将要创建一个学生对象(tech.student)和一些基本的属性,并将用form和tree视图将其展示出来: 一. 创建tech.st ...

  2. Lintcode: Remove Node in Binary Search Tree

    iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...

  3. Remove Node in Binary Search Tree 解答

    从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树 ...

  4. 【Lintcode】087.Remove Node in Binary Search Tree

    题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given v ...

  5. Git CMD - rm: Remove files from the working tree and from the index

    命令格式 git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>…​ 命令参 ...

  6. [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 ...

  7. ExtJS笔记 Tree

    The Tree Panel Component is one of the most versatile Components in Ext JS and is an excellent tool ...

  8. Django Model数据访问Making queries

    创建完Model之后, Django 自动为你提供一套数据库抽象层的API,利用它可以完成创建,提取,更新,删除对象的操作. 以下面的Model为例: class Blog(models.Model) ...

  9. django学习之Model(一)

    认认真真学Django,从现在开始. 学习资料来源于官方网站:https://docs.djangoproject.com/en/1.6/ 1-新建一个models.py from django.db ...

随机推荐

  1. java实现登录的验证码和猜数字游戏_图形化界面

    实验任务四 1,出现设计思想 (1)先定义文本框.密码框和验证码框的组件 (2)定义面板和按钮的个数 (3)定义公有的虚构方法,通过对象实例化来调用 (4)利用Random类来实现生成0-9的随机数 ...

  2. IDEA Maven Web项目 clone到本地导入到Eclipse中,启动服务器的时候会出现这个错误:SEVERE: Exception starting filter [hiddenHttpMethodFilter]

    背景(Background): 我将一个IDEA的maven web项目clone到本地,并导入到Eclipse中. I imported a MAVEN WEB project which was ...

  3. 报错:SyntaxError: Non-ASCII character '\xe4' in file

    SyntaxError: Non-ASCII character '\xe1' in file recommendation.py on line 1, but no encoding declare ...

  4. javaScript将string转换成array,并将汉字按汉语拼音排序方法

    亲测,代码如下: var str = '中华人民共和国民主富强': var arr = str.split("");//字符串装换数组方法一 //arr = str.replace ...

  5. 【BZOJ 1433】[ZJOI2009]假期的宿舍

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把每个人都分为左边和右边两个人 xi,yi 如果第i个人不回家或者是外校学生 那么它可以和他认识的人连一条容量为1的边(前提是这个认 ...

  6. Java基础学习总结(47)——JAVA输入输出流再回忆

    一.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列. Java的I/O流提供了 ...

  7. POJ——T 3159 Candies

    http://poj.org/problem?id=3159 Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 33328   ...

  8. android 读取xml

    在有些应用中,有一点小数据.直接存储在XML就是.实现较为简单, 1.xml文件放入asset目录.结构如: <?xml version="1.0" encoding=&qu ...

  9. Oracle 12c agent install for windows

    在Oracle EM12c 中部署agent的方法分两种,一种是通过EM12c的控制台通过ssh直接把agent"推送"安装到被管理端.这样的方法在linux平台的OMS和被管理端 ...

  10. war包放入tomcat

    1.找到tomcat的安装路径(如:D:\example\download\set\apache-tomcat-7.0.23) 2.D:\example\download\set\apache-tom ...