如何在 ExtJS 中加入模型?

How can I join models in ExtJS?

在 ExtJS 6.02 中,我想将 2 个或更多模型合并到一个商店中。

一对一关系:


给定的用户只能与数据库中的单个用户详细信息行关联:

"users": [
    {
      "id": 1,
      "name": "Philip J. Fry",
      "userDetail": 1
    },
    {
      "id": 2,
      "name": "Hubert Farnsworth",
      "userDetail": 2
    },
    {
      "id": 3,
      "name": "Turanga Leela",
      "userDetail": 3
    },
    {
      "id": 4,
      "name": "Amy Wong",
      "userDetail": 4
    }
]
"usersDetails": [
    {
      "id": 1,
      "email": "jfry@a.com",
      "sex": "m"
    },
    {
      "id": 2,
      "email": "hubert@a.com",
      "sex": "m"
    },
    {
      "id": 3,
      "email": "leela@a.com",
      "sex": "f"
    },
    {
      "id": 4,
      "email": "amy@a.com",
      "sex": "m"
    }
]

我想在商店里买到这个:

"users": [
    {
      "id": 1,
      "name": "Philip J. Fry",
      "email": "jfry@a.com",
      "sex": "m"
    },
    {
      "id": 2,
      "name": "Hubert Farnsworth",
      "email": "hubert@a.com",
      "sex": "m"
    },
    {
      "id": 3,
      "name": "Turanga Leela",
      "email": "leela@a.com",
      "sex": "f"
    },
    {
      "id": 4,
      "name": "Amy Wong",
      "email": "amy@a.com",
      "sex": "m"
    }
]

1对多关系:


假设用户可以在 BD 中拥有多个帖子:

"users": [
    {
      "id": 1,
      "name": "Philip J. Fry",
      "userDetail": 1
    },
    {
      "id": 2,
      "name": "Hubert Farnsworth",
      "userDetail": 2
    },
    {
      "id": 3,
      "name": "Turanga Leela",
      "userDetail": 3
    },
    {
      "id": 4,
      "name": "Amy Wong",
      "userDetail": 4
    }
]
"posts": [
    {
      "id": 1,
      "user": 1,
      "title": "Post 1 title",
      "body": "Post 1 body"
    },
    {
      "id": 2,
      "user": 1,
      "title": "Post 2 title",
      "body": "Post 2 body"
    },
    {
      "id": 3,
      "user": 2,
      "title": "Post 3 title",
      "body": "Post 3 body"
    },
    {
      "id": 4,
      "user": 3,
      "title": "Post 4 title",
      "body": "Post 4 body"
    }
]

我想要一家商店包含:

"items": [
    {
      "id": 1,
      "name": "Philip J. Fry",
      "posts": [
          {
            "id": 1,
            "title": "Post 1 title",
            "body": "Post 1 body"
          },
          {
            "id": 2,
            "title": "Post 2 title",
            "body": "Post 2 body"
          }
      ]
    },
    {
      "id": 2,
      "name": "Hubert Farnsworth",
      "posts": [
          {
            "id": 3,
            "user": 2,
            "title": "Post 3 title",
            "body": "Post 3 body"
          }
      ]
    },
    {
      "id": 3,
      "name": "Turanga Leela",
      "userDetail": 3,
      "posts": [
          {
            "id": 4,
            "user": 3,
            "title": "Post 4 title",
            "body": "Post 4 body"
          }
      ]
    },
    {
      "id": 4,
      "name": "Amy Wong",
      "posts": []
    }
]

多对一关系:


这实际上是我的实际问题,我有一个包含多个项目的数据库 table,每个项目只能匹配另一个 table 中的一个项目。 应该与一对多相同。

多对多关系:


基本和我猜的 1 对多一样?

我看过这个解决方案:https://fiddle.sencha.com/#fiddle/1hs9&view/editor它并没有涵盖所有内容,也许还有更好的选择。

根据 Sencha 支持:

The concept of joining records doesn't really exist within ExtJS but you can, by making use of renderers, access and print record association data.

I have a simple fiddle of your example above showing you how you may achieve this. Please note that dynamic loading of association (which you have in your example) makes using renderers a bit tricky, but not impossible, as the loading of the association info is asynchronous in nature and the renderer cannot wait for the response. The work around is to refresh the entire grid when all association stores have finished loading.

https://fiddle.sencha.com/#fiddle/3d1v&view/editor