递归http-api调用后的消息订阅者

时间:2017-08-28 14:35:47

标签: javascript angular recursion

我想获取节点列表以创建所述节点对象的数组以显示层次结构。基础数据/结构如下所示:

ROOT (ID=1)
|--NODE (ID=2)
|  └--NODE (ID=4)
|     └--NODE (ID=11)
└--NODE (ID=3)
   |--NODE (ID=5)
   |--NODE (ID=6)
   |--NODE (ID=7)
   └--NODE (ID=8)
      |--NODE (ID=9)
      └--NODE (ID=10)  

您有一个根节点,其子节点也包含子节点。包含ROOT节点,树中有3个阶段。我创建了一个REST-API,它返回给定节点内的子节点。我还尝试在REST结构中对此层次结构进行建模,因此调用如下所示:

TYPE        PATH                      RESULT CHILD IDs
GET         /1/nodes                  2, 3
GET         /1/nodes/2/nodes          4
GET         /1/nodes/3/nodes          5, 6, 7, 8
GET         /1/nodes/3/nodes/8/nodes  9, 10 

为了构建一棵树,我尝试使用递归模式,如下所示:

    this.subject.next(this.getNodes('/1/nodes'); /*root-uri*/

    getNodes(uri) {
      const nodeList: Node[] = [];
      http.get(path).subscribe(data => {
        for(const obj of data.json()) {
            let node = new Node();
            //map data to Node e.g node.is = data.id

            //get the children with the nested call
            node.children = getNodes(uri + '/' + node.id + '/nodes'); 
            nodeList.push(node);
        }
    }
    return nodeList;
}  

所以我的问题是:
如何从递归http调用创建节点对象数组并向订阅者发送消息,以便订阅者只接收正确结构中的完整节点数组

修改

这就是节点模型的样子

export class Node {
  uuid: string;
  label: string;
  parentID: string;
  version: number;
  addableFlag: boolean;
  sectionFlag: boolean;
  children: Node[];
}

1 个答案:

答案 0 :(得分:0)

您需要在from Bio import SeqIO import glob fasta_file = "sequences.txt" for filename in glob.glob('*.txt'): wanted = set() with open(filename) as f: for line in f: line = line.strip() if line != "": wanted.add(line) fasta_sequences = SeqIO.parse(open(fasta_file),'fasta') with open(result_file, "w") as f: for seq in fasta_sequences: if seq.id in wanted: SeqIO.write([seq], f, "fasta") 范围内设置this.subject值。因此,在设置值之前,它具有正确结构的完整列表。

subscribe()

这将有效,因为它设置了this.getChildren('/1/nodes'); /*root-uri*/ getNodes(uri) { const nodeList: Node[] = []; http.get(path).subscribe(data => { for (const obj of data.json()) { let node = new Node(); //map data to Node e.g node.is = data.id //get the children with the nested call node.children = getNodes(uri + '/' + node.id + '/nodes'); nodeList.push(node); } this.subject.next(nodeList); /*root-uri*/ } // Return would send empty data as this line get executed before data arrives // return nodeList; }的数据时的值。