gpt4 book ai didi

json - 如何在 Flux 应用程序中处理嵌套 API 响应?

转载 作者:行者123 更新时间:2023-12-02 10:53:20 24 4
gpt4 key购买 nike

我正在将现有的应用程序移植到 Flux,但我对一个主题有点困惑。假设我有几个返回两层或三层嵌套对象的 API 端点。

例如,GET/articles 可能会返回 schema 的 JSON 响应

articles: article*

article: {
author: user,
likers: user*
primary_collection: collection?
collections: collection*
}

collection: {
curator: user
}

如您所见,存在不同嵌套级别的各种用户:

  • 文章[i].author
  • 文章[i].likers[i]
  • articles[i].primaryCollection.curator
  • 文章[i].collections[i].curator

如果我想在每次获取文章时使用新数据更新 UserStore,我必须编写一个巨大的方法来检查文章 API 响应上的所有嵌套实体。此外,还会存在大量重复,因为还有其他具有不同架构的 API 端点,有时文章会嵌入用户内部(例如 GET/user/published)。

Flux 存储是否有一种更简洁的方法来从所有 API 响应中提取嵌套实体?

最佳答案

Jing Chen建议的方法(Flux 创建者和传播者之一)的目标是在 API 响应到达商店之前将其扁平化。我写了一个小型库,它的作用就是:它标准化

[{
id: 1,
title: 'Some Article',
author: {
id: 1,
name: 'Dan'
}
}, {
id: 2,
title: 'Other Article',
author: {
id: 1,
name: 'Dan'
}
}]

{
result: [1, 2],
entities: {
articles: {
1: {
id: 1,
title: 'Some Article',
author: 1
},
2: {
id: 2,
title: 'Other Article',
author: 1
}
},
users: {
1: {
id: 1,
name: 'Dan'
}
}
}
}

(注意没有重复,结构是扁平的。)

<强> Normalizr 让您:

  • 将实体嵌套在其他实体、对象和数组中
  • 组合实体架构来表达任何类型的 API 响应
  • 自动合并具有相同 ID 的实体(如果不同,则会发出警告)
  • 使用自定义 ID 属性(例如 slug)

要使用它,您需要定义实体和嵌套规则并使用它们来转换 JSON:

var normalizr = require('normalizr'),
normalize = normalizr.normalize,
Schema = normalizr.Schema,
arrayOf = normalizr.arrayOf;

// First, define a schema:

var article = new Schema('articles'),
user = new Schema('users'),
collection = new Schema('collections');

// Define nesting rules:

article.define({
author: user,
collections: arrayOf(collection)
});

collection.define({
curator: user
});


// Usage:

// Normalize articles
var articlesJSON = getArticleArray(),
normalized = normalize(articlesJSON, arrayOf(article));

// Normalize users
var usersJSON = getUsersArray(),
normalized = normalize(usersJSON, arrayOf(user));

// Normalize single article
var articleJSON = getArticle(),
normalized = normalize(articleJSON, article);

这允许您在将任何 XHR 响应传递给 Flux Dispatcher 之前对其进行标准化。商店只需要从相应的字典中更新自己:

// UserStore

UserStore.dispatchToken = AppDispatcher.register(function (payload) {
var action = payload.action;

switch (action.type) {
// you can add any normalized API here since that contains users:
case ActionTypes.RECEIVE_ARTICLES:
case ActionTypes.RECEIVE_USERS:

// Users will always be gathered in action.entities.users
mergeInto(_users, action.entities.users);
UserStore.emitChange();
break;
}
});


// ArticleStore

AppDispatcher.register(function (payload) {
var action = payload.action;

switch (action.type) {
// you can add any normalized API here since that contains articles:
case ActionTypes.RECEIVE_ARTICLES:

// Wait for UserStore to digest users
AppDispatcher.waitFor([UserStore.dispatchToken]);

// Articles will always be gathered in action.entities.articles
mergeInto(_articles, action.entities.articles);
ArticleStore.emitChange();
break;
}
});

关于json - 如何在 Flux 应用程序中处理嵌套 API 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com