0

I try to make an articles app.
1

based on angular and ionic but the problem I face is the programming structure for Behaviorsubject for the serializer nested data. My backend uses django-rest-framework

*I successfully perform "CRUD" operations (I.e. live updates from server and behavior subject) with basic data I.e. only with author data or article data but failed with nested serialize data *

All I want the best practice of behavior subject for the nested data of authors and articles

Question.) Make an Behaviorsubject for one URLs http:local host:8000/v1/authors/ Which Uses nested serializer and returns both authors and articles data Below image ?

Below image shows authors array data with articles data of second model as nested (Thanks in advance)

[1]:

Code of my 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)
      )        
    }
  )
)

Am i doing right ?

*Right now i just split the data after recieving from backend (by extracting the article's data from author's dictionary) and store it separetely

*articlesInterface for holding all the article authorsInterface for holding all the authors

Then inject article's service into authors's service don't know it's a best practice or not ?

I made two BehaviorSubject

Shlok Nangia
  • 2,355
  • 3
  • 16
  • 24
HeRo
  • 21
  • 1
  • 5
  • You want to use BehaviourSubject to pass data? – isAif May 17 '20 at 17:19
  • Provide the code for 'CRUD' operations which you have done – isAif May 17 '20 at 17:20
  • Brother i update the post with my "code" as you asking,glad if you help me :) – HeRo May 19 '20 at 09:21
  • Do you want to post nested data to backend or read nested data? I am having difficulty understanding your question. – isAif May 19 '20 at 09:41
  • I am comfortable with the "CRUD" operations by making one BehaviorSubject but this time how can i handle the **nested data** coming from server having two models articles and authors as shown in pictures. **Question** - *How can i make the **structure** of **service.ts** and **Behavior Subject** (*should i make two service file or two BehaviorSubject one for articles and one for authors* ) as per data coming from backend. Goal here not to send request to the server for data which can be cached. **How to handle or cached nested data properly in order to re-use it.** – HeRo May 19 '20 at 11:45

1 Answers1

0

You should include Article inside the Author model:

export interface Article {
  // fields for articles
}

export interface Author {
  // fields for autor
  articles: Article[]
}

This will ensure that the data you are getting from authors endpoint will match with the Author model and for articles endpoint use Article model.

There is no need to split the data coming from authors endpoint.

If there is a requirement to list all articles irrespective of author, then create a new url which returns all the articles and use Article model for that.

isAif
  • 2,126
  • 5
  • 22
  • 34
  • Am i thinking wrong ? That if entire articles data already coming from authors data, so why we calls **api** for article section , just use behaviorsubject to store it and call it everytime we need------------------------------------------------------------------------ I successfully retrieve the author's data – HeRo May 20 '20 at 04:45
  • If the articles data belongs to a author, there is no need to separate it. – isAif May 20 '20 at 04:49
  • Thank u for ur reply , Any blog or example which shows nested data calling with proper Observable for caching it ? – HeRo May 20 '20 at 04:54
  • Actually i reliaze,i have to calls two api one for **articles/** and second for **authors/** But i think calls all the data once using **realated nested serialzer** _and store it_ for later use and no need to calls aritlces data . ** I think that was an mistake ** – HeRo May 24 '20 at 04:33