问题描述
我是 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 方法一样吗?
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 (将#修改为@)