[MST] Remove Model Instances from the Tree
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
getParentto find the parent of a model instance - Using
destroyto 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的更多相关文章
- Odoo 二次开发教程(三)-第一个Model及Form、Tree视图
创建完我们的模块,接下来我们就要为我们的模块添加一些对象.今天我们将要创建一个学生对象(tech.student)和一些基本的属性,并将用form和tree视图将其展示出来: 一. 创建tech.st ...
- 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. ...
- Remove Node in Binary Search Tree 解答
从BST中移除一个节点是比较复杂的问题,需要分好几种情况讨论. 如这篇文章,就讨论了删除节点 1.有无左右子树 2.只有右子树 3.只有左子树 三种情况. 一种简单些的思维是只考虑删除节点是否有右子树 ...
- 【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 ...
- Git CMD - rm: Remove files from the working tree and from the index
命令格式 git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>… 命令参 ...
- [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 ...
- ExtJS笔记 Tree
The Tree Panel Component is one of the most versatile Components in Ext JS and is an excellent tool ...
- Django Model数据访问Making queries
创建完Model之后, Django 自动为你提供一套数据库抽象层的API,利用它可以完成创建,提取,更新,删除对象的操作. 以下面的Model为例: class Blog(models.Model) ...
- django学习之Model(一)
认认真真学Django,从现在开始. 学习资料来源于官方网站:https://docs.djangoproject.com/en/1.6/ 1-新建一个models.py from django.db ...
随机推荐
- 用 JavaScript 实现简单拼图游戏
本篇主要讲解,如何利用原生的 JavaScript 来实现一个简单的拼图小游戏. 线上体验地址:拼图 一.游戏的基础逻辑 想用一门语言来开发游戏,必须先了解如何使用这门语言来实现一些基础逻辑,比如图像 ...
- 2019-02-25 SQL:cast(itemvalue as decimal(19,4))
1.Operand data type nvarchar(max) is invalid for sum operator 要转换格式 2.Conversion failed when convert ...
- maven 测试写入JRE参数
项目在测试时碰到一个问题,就是JVM加载参数的问题. web项目本身在注入配置信息的时候,读取的是本地的配置文件,但是配置文件的位置是卸载tomcat 里面配置的JAVA_OPTS里面的. 问题出现了 ...
- Spring的ApplicationContextAware接口的作用
ApplicationContextAware接口: 当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean.换句话说,就是这个类可以直接获取Spr ...
- Intellij IDEA插件 - Scroll From Source
Intellij IDEA插件 - Scroll From Source 学习了:http://blog.csdn.net/luonanqin/article/details/41088171 可以自 ...
- 手动配置IP网络
1. #vi /etc/sysconfig/network-scripts/ifcfg-ens33 ifcfg-ens33 是网卡的名字,根据自己的计算机 2. 修改配置 ONBOOT=no改为ONB ...
- sql-查看执行计划的方法
sql执行计划:把SQL语句拆分为每个的操作步骤组合,按照一定的顺序执行得出结果,查看并看懂执行计划是调优的关键步骤 查看执行计划的方法 DBMS_XPLAN包 sql*plus AUTO trace ...
- 洛谷P1339 [USACO09OCT]热浪Heat Wave(最短路)
题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...
- Debian/Linux 下无线网卡驱动的安装
我的 PC 型号是 Acer V3-572G, 安装了 Debian 后, 发现只能通过有线网络上网, 无法识别无线网卡, 以下是解决的过程(不局限于此型号 PC): 在命令行键入 lspci , 得 ...
- 获取新浪微博的Access_token
最近想爬取新浪微博的评论,百度了一下,有个新浪开放平台提供了这个API 于是按照它的说明,去获取Access_token: 1.点击微链接 2.立即创建微链接 3.选择网页应用 4.填写信息后提交 5 ...