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. 小试牛刀之sort()排序的实现

    受大学室友的鼓动,我也打算利用公众平台来记录自己的前端知识积累,同时呢,自己总结的东西,总归会有局限性,希望小伙伴能给我指点迷津.知识就是一张巨大的网,作为一名摸不清头绪的入学者,唯一能做的事情就是吐 ...

  2. STM32 软件模拟 IIC 代码,标准库、HAL库可用

    #ifndef _IIC_H #define _IIC_H #include "stdio.h" #include "stm32f1xx_hal.h" /* 定 ...

  3. java源码之HashSet

    1,HashSet介绍 1)HashSet 是一个没有重复元素的集合.2)它是由HashMap实现的,不保证元素的顺序,而且HashSet允许使用 null 元素.3)HashSet是非同步的.如果多 ...

  4. arXiv 2015深度学习年度十大论文

    由康奈尔大学运营维护着的arXiv网站,是一个在学术论文还未被出版时就将之向所有人开放的地方.这里汇聚了无数科学领域中最前沿的研究,机器学习也包括在内.它反映了学术界当前的整体趋势,我们看到,近来发布 ...

  5. ASP.NET-JSON.NET技巧

    第一个技巧,字符串转JSON 单条的json数据可以使用JObject.Parse将对象转化成JObject对象,你可以接着使用JsonConvert.SerializeObject方法把这个对象序列 ...

  6. javaweb学习总结(六)——Servlet开发(三) 常见问题疑问

    [1]response.getWriter().write()与out.print()的区别http://blog.csdn.net/javaloveiphone/article/details/81 ...

  7. 用exp命令按用户导出的DMP文件缺少表,某些表是空表

    1.用以下这句查找空表并生成执行命令 select 'alter table '||table_name||' allocate extent;' from user_tables where num ...

  8. ubuntu12.04下CKermit与开发板交互环境搭建

    CKermit蛮好的一个调试工具!就像在windows下的telnet,但是还是折腾了一下,现在看来,非常容易,其实我主要是在开发板为正常工作的情况下,以为是CKermit的问题,其实是我开发板开机设 ...

  9. 092117-6265-01.dmp 蓝屏日志文件

    Microsoft (R) Windows Debugger Version 10.0.15063.137 AMD64 Copyright (c) Microsoft Corporation. All ...

  10. MyBatis数据持久化(二)建立数据库会话

    上篇文章中介绍了我们使用mybatis前的准备工作,并在程序中通过jdbc与mysql数据库成功建立连接,接下来介绍如何使用mybatis与数据库建立会话.我们需要以下几个步骤: 1.在build p ...