zoukankan      html  css  js  c++  java
  • [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 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)
            }
        }))
  • 相关阅读:
    Tomcat虚拟目录的映射方式
    Linux常用命令
    java断点调试
    破解MyEclipse
    JS判断浏览器
    css3 box-sizing详解。
    this-使用call . apply
    this-内部函数
    this-对象方法调用
    this-纯函数
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8369681.html
Copyright © 2011-2022 走看看