微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mobx 商店查询

如何解决mobx 商店查询

我是 Mobx 的新手,并尝试通过阅读 this documentation 来创建数据存储。我设置了传输层以与我的数据库通信以进行 CRUD 操作。

class transportLayer{
  async createData(todo){...}
  async fetchData(){...}
  async updateData(updatedTodo){...}
  async deleteData(id){...}
}

但在 Mobx 文档中,

this.transportLayer.onReceivetodoUpdate(updatedTodo =>
            this.updatetodoFromServer(updatedTodo)
        )

我不知道如何在传输层创建 onReceivetodoUpdate 方法。 onReceivetodoUpdate 方法和 updateData 方法一样吗?

另外,我还有一个关于文档中的域对象 Todo 的问题。

export class Todo {
    id = null // Unique id of this Todo,immutable.
    completed = false
    task = ""
    author = null // Reference to an Author object (from the authorStore).
    store = null
    autoSave = true // Indicator for submitting changes in this Todo to the server.
    saveHandler = null // disposer of the side effect auto-saving this Todo (dispose).

    constructor(store,id = uuid.v4()) {
        makeAutoObservable(this,{
            id: false,store: false,autoSave: false,saveHandler: false,dispose: false
        })
        this.store = store
        this.id = id

        this.saveHandler = reaction(
            () => this.asJson,// Observe everything that is used in the JSON.
            json => {
                // If autoSave is true,send JSON to the server.
                if (this.autoSave) {
                    this.store.transportLayer.savetodo(json)
                }
            }
        )
    }

我知道 Mobx 反应在指定的状态改变后被触发,但我不明白 this.asJson() 如何触发反应 saveHandler。另外,我不明白这行代码中的 savetodo 方法this.store.transportLayer.savetodo(json) 是更新 todo方法吗?我研究了这个文档几天,但仍然感到困惑。如果有人能向我解释一下,我将不胜感激。谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。