【问题标题】:Mobx-state-tree create form with types.identifier field on modelMobx-state-tree 在模型上创建带有 types.identifier 字段的表单
【发布时间】:2018-11-07 14:49:05
【问题描述】:

我最近开始使用 mobx-state-tree,我有一个实际问题。

我有一个模型,它有一个 types.identifier 字段,这是资源的数据库 ID,当我查询现有的东西时,它会被填充。

但是,当我创建一个新实例时,在 example that Michel has on egghead 之后,我需要在初始状态下将初始 id 传递给我的 MyModel.create(),但是,只有在我发布创建后才能知道此 ID到 API 并获取生成的资源。

我使用 mobx-state-tree 搜索了一个简单的 crud 示例,但找不到(建议?)。

这里的最佳做法是什么?我应该执行 `MyModel.create({ id: 'foobar' }) 并在我发布到 API 时将其清除(并在我从 API 获得响应后更新实例)?

【问题讨论】:

    标签: mobx mobx-state-tree


    【解决方案1】:

    这是 mobx-state-tree 当前设计的限制。 Identifiers are immutable.

    我见过的解决此问题的一种策略是将持久层的id 存储在与types.identifier 字段不同的字段中。然后,您将使用像 uuid 这样的库来生成 types.identifier 值:

    import { v4 } from "node-uuid"
    const Box = types
        .model("Box", {
            id: types.identifier,
            name: "hal",
            x: 0,
            y: 0
        })
    const box = Box.create({ 'hal', 10, 10, id: v4() })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      相关资源
      最近更新 更多