【发布时间】:2020-09-03 10:05:09
【问题描述】:
我尝试制作文章应用。
基于角度和离子,但我面临的问题是序列化器嵌套数据的 Behaviorsubject 的编程结构。 我的后端使用 django-rest-framework
*我使用基本数据成功执行“CRUD”操作(即来自服务器和行为主体的实时更新)仅使用作者数据或文章数据但嵌套序列化数据失败 *
All I want the best practice of behavior subject for the nested data of authors and article
问题。)为一个 URL 创建一个 Behaviorsubject http:本地主机:8000/v1/authors/ 它使用嵌套序列化程序并返回作者和文章数据 下图 ?
下图显示了第二个模型的作者数组数据 文章数据嵌套 (提前致谢)
[1]:
我的 articles.service.ts
的代码 extractArticles(data : AuthorsInterface[]){
/*
* Extracting then Returns
* [] of Articles Data
* from Parent Data
*/
let localArray = []
if (data.length > 0){
for( let x in data ){
if ( data[x].articles.length > 0 ){
for( let y in data[x].articles){
localArray.push(data[x].articles[y])
}
}
}
return this.articlesInterface.concat(localArray)
}
}
getParents() {
/*
* Calling REST-API
* Extract Authors & Articles
* from the parent Data
* & cached them
*/
return this.http.get<AuthorsInterface[]>(this.AUTHOR_URL)
.pipe(
map(mapData=>{
**here comes the combined data of articles and authors **
*as shown in image*
this.articlesBehavior.next(
this.extractArticles(mapData)
)
}
)
)
我做得对吗?
*现在我只是在从后端接收数据后拆分数据 (通过从作者字典中提取文章数据) 并单独存放
*articlesInterface 用于存放所有文章 authorsInterface 用于保存所有作者
然后将文章的服务注入作者的服务不知道这是不是最佳实践?
我做了两个 BehaviorSubject
【问题讨论】:
-
你想使用 BehaviourSubject 来传递数据吗?
-
提供您已完成的“CRUD”操作的代码
-
兄弟,我按照你的要求用我的“代码”更新了帖子,很高兴你能帮助我:)
-
您想将嵌套数据发布到后端还是读取嵌套数据?我很难理解你的问题。
-
我通过创建一个 BehaviorSubject 对“CRUD”操作感到满意,但这次我如何处理来自具有两个模型文章和作者的服务器的 嵌套数据,如图所示图片。 问题 - 我怎样才能使 service.ts 和 Behavior Subject 的 结构 (*我应该根据来自后端的数据,制作两个服务文件或两个 BehaviorSubject 一个用于文章,一个用于作者)。这里的目标是不要向服务器发送请求以获取可以缓存的数据。 如何正确处理或缓存嵌套数据以便重复使用。
标签: angular django-rest-framework django-serializer behaviorsubject rxjs-observables