Создайте массив объектов, просматривая наблюдаемый - PullRequest
0 голосов
/ 04 февраля 2019

У меня есть Angular-компонент, который подписывается на наблюдаемое, возвращаемое Angular Service.

Компонент: help.component.ts

import { WikiService } from '../../../services/wiki.service';

import { WikiTree } from '../../../interfaces/WikiTree';

export class HelpComponent implements OnInit {

    wikiTree$: Observable<WikiTree>

    public infoCards: Array<Object>;

    constructor(private wikiService: WikiService) {}

    ngOnInit() {
            this.wikiTree$ = this.wikiService.GetWikiHierarchy();
            this.wikiTree$.subscribe(()=>{
               //TODO: Create infoCards array by iterating through the results array inside the wikiTree observable.
            }
        }
}

Наблюдаемая wikiTree$ имеет следующий JSON, преобразованный в TypeScript:

{
    "page": {
        "results": [
            {
                "id": "123456789",
                "type": "page",
                "status": "current",
                "title": "Start here",
                "extensions": {
                    "position": 0
                },
                "_links": {
                    "webui": "/display/MYSPACE/Start+here",
                    "edit": "/pages/resumedraft.action?draftId=123456789",
                    "tinyui": "/x/BQD2Mw",
                    "self": "https://wiki.abc.com/rest/api/content/123456789"
                },
                "_expandable": {
                    "container": "/rest/api/space/MYSPACE",
                    "metadata": "",
                    "operations": "",
                    "children": "/rest/api/content/123456789/child",
                    "restrictions": "/rest/api/content/123456789/restriction/byOperation",
                    "history": "/rest/api/content/123456789/history",
                    "ancestors": "",
                    "body": "",
                    "version": "",
                    "descendants": "/rest/api/content/123456789/descendant",
                    "space": "/rest/api/space/MYSPACE"
                }
            },
            {
                "id": "567890123",
                "type": "page",
                "status": "current",
                "title": "FAQ",
                "extensions": {
                    "position": 1
                },
                "_links": {
                    "webui": "/display/MYSPACE/FAQ",
                    "edit": "/pages/resumedraft.action?draftId=567890123",
                    "tinyui": "/x/HQD2Mw",
                    "self": "https://wiki.abc.com/rest/api/content/567890123"
                },
                "_expandable": {
                    "container": "/rest/api/space/MYSPACE",
                    "metadata": "",
                    "operations": "",
                    "children": "/rest/api/content/567890123/child",
                    "restrictions": "/rest/api/content/567890123/restriction/byOperation",
                    "history": "/rest/api/content/567890123/history",
                    "ancestors": "",
                    "body": "",
                    "version": "",
                    "descendants": "/rest/api/content/567890123/descendant",
                    "space": "/rest/api/space/MYSPACE"
                }
            }
        ],
        "start": 0,
        "limit": 25,
        "size": 2,
        "_links": {
            "self": "https://wiki.abc.com/rest/api/content/998877665/child/page"
        }
    },
    "_links": {
        "base": "https://wiki.abc.com",
        "context": "",
        "self": "https://wiki.abc.com/rest/api/content/998877665/child"
    },
    "_expandable": {
        "attachment": "/rest/api/content/998877665/child/attachment",
        "comment": "/rest/api/content/998877665/child/comment"
    }
}

TypeScript: WikiTree.ts

export interface WikiTree {
    page: Page;
    _links: Links;
    _expandable: Expandable;
  }
  export interface Page {
    results?: (ResultsEntity)[] | null;
    start: number;
    limit: number;
    size: number;
    _links: Links1;
  }
  export interface ResultsEntity {
    id: string;
    type: string;
    status: string;
    title: string;
    extensions: Extensions;
    _links: Links2;
    _expandable: Expandable1;
  }
  export interface Extensions {
    position: number;
  }
  export interface Links2 {
    webui: string;
    edit: string;
    tinyui: string;
    self: string;
  }
  export interface Expandable1 {
    container: string;
    metadata: string;
    operations: string;
    children: string;
    restrictions: string;
    history: string;
    ancestors: string;
    body: string;
    version: string;
    descendants: string;
    space: string;
  }
  export interface Links1 {
    self: string;
  }
  export interface Links {
    base: string;
    context: string;
    self: string;
  }
  export interface Expandable {
    attachment: string;
    comment: string;
  }

Я быхотел бы перебрать массив "page" -> "results" в wikiTree$, наблюдаемом в методе подписки, и создать массив объектов с именем infoCards, как определено в компоненте с этим JSON, с некоторыми значениями изwikiTree$ наблюдаемый.:

[{
        "title": "Start here",
        "titleLink": "/page/wiki/123456789",
        "children": []
    },
    {
        "title": "FAQ",
        "titleLink": "/page/wiki/567890123",
        "children": []
    }
    ]

Как мне это сделать?

Обновление:

Компонент InfoCard

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-info-card',
  templateUrl: './info-card.component.html',
  styleUrls: ['./info-card.component.css']
})
export class InfoCardComponent implements OnInit {
  @Input('infoCard') infoCard: Array<Object>;

  constructor() { }

  ngOnInit() {
  }

}

Ответы [ 2 ]

0 голосов
/ 04 февраля 2019

Следующая подписка будет создавать новые объекты на основе структуры, предоставленной вами при поступлении новых данных. Я предполагаю, что вам нужны только title и titleLink в указанном формате, а массив children получен из другой службы?

this.wikiTree$.subscribe( res => {
    const newInfoCards = res.page.results.map(result => ({
        "title": result.title,
        "titleLink": "/page/wiki/"+ result._links.self.split('/').pop(),
        "children": []
    }),
    this.infoCards.concat(newInfoCards);
})
0 голосов
/ 04 февраля 2019

Если бы я столкнулся с этим, то решил бы это следующим образом:

Сначала я бы создал Модель (Класс), как модели в вашем WikiTree.ts. для инфо-карты.Я делаю это Class для создания его объекта внутри метода подписки -

export class InfoCard{
        title: string;
        titleLink: string;
        children: string;
  }

После этого в Component: help.component.ts Я бы объявил инфо-карты какthis-

infoCards: InfoCard[]=[];

Очевидно, вы должны импортировать InfoCard из WikiTree.ts -

import { InfoCard} from '../../../interfaces/WikiTree';

Затем внутри метода подписки,Я бы отобразил данные, поступающие в виде Observable-

    this.wikiTree$.subscribe(data =>{
      data.page.results.map(
       result=>{
          let newInfoCard = new InfoCard();
          newInfoCard.title = result.title;
          newInfoCard.children= result._expandable.children;
          newInfoCard.titleLink= result._links.self;
          this.infoCards = [this.infoCards, ...newInfoCard]
        }
       )
     }

Когда вы добавили компонентную часть, вы можете попробовать это также:

Инициализировать массив-

infoCards: Array<Object>= new Array();

Возможно, есть еще варианты, чтобы сделать это.Я делюсь своим.Дайте мне знать, если вы ничего не понимаете, у меня есть вопрос или ошибки, которые я совершил.

    forkJoin(
        this.wikiService.GetWikiHierarchy(),
        this.wikiService.GetWikiChildren()
    ).subscribe(([data, children]) => {
        data.page.results.map(
            result => {
                this.infoCards.push({
                    'title': result.title,
                    'titleLink': result._links.self,
                    'children': children.whatEver
                });
            }
        )
    });

И, если звонок детей зависит от предыдущего звонка в службу поддержки, вы можете попробовать что-то вроде этого -

  this.wikiService.GetWikiHierarchy().subscribe(
    data=>{
      data.page.results.map(
        result=>{
          this.wikiService.GetWikiChildren(result.id).subscribe(
            child=>{
                this.infoCards.push({
                  'title': result.title,
                  'titleLink': result._links.self,
                  'children': child.whatEver
                    });
            }
          )
        }
      )
    }
  )
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...