Execute typescript functions in a certain order (Angular & DjangoRest)

0

I am new with Angular and I am trying to execute certain functions in a specific order to be able to present them in the html.component.

I have the following model:

What I need is to get the levels, both "Competence" and "Result" ... for this I thought to do it in the following order:

For levels of results

  • Get "Result" ID
  • Get ID of "Taxonomy" from "Result"
  • Get Level IDs of the "Taxonomy"
  • For skill levels

  • Get ID of "Norma"
  • Get ID of "Taxonomy"
  • Obtain level IDs for "Taxonmy"
  • That's why I thought to use asynchrony in Angular with typescript, but I can determine how to code it.

    This is my component.ts

    import { Component, OnInit } from '@angular/core';
    import { CursoService } from '../services/curso/curso.service';
    import { Curso } from '../services/curso/model';
    import { Eje_curricular } from '../services/eje-curricular/model';
    import { EjeCurricularService } from '../services/eje-curricular/eje-curricular.service';
    import { Carrera } from '../services/carrera/model';
    import { CarreraService } from '../services/carrera/carrera.service';
    import { Resultado_aprendizaje } from '../services/resultado-aprendizaje/model';
    import { ResultadoAprendizajeService } from '../services/resultado-aprendizaje/resultado-aprendizaje.service';
    import { Norma } from '../services/norma/model';
    import { NormaService } from '../services/norma/norma.service';
    import { Competencia } from '../services/competencia/model';
    import { CompetenciaService } from '../services/competencia/competencia.service';
    import { Taxonomia } from '../services/taxonomia/model';
    import { TaxonomiaService } from '../services/taxonomia/taxonomia.service';
    import { ActivatedRoute} from '@angular/router';
    import { Router } from '@angular/router';
    import { resolve } from 'q';
    
    
    export interface Nivel {
      valor: string;
    }
    
    @Component({
      selector: 'app-curso',
      templateUrl: './curso.component.html',
      styleUrls: ['./curso.component.scss']
    })
    export class CursoComponent implements OnInit {
    
      niveles: Nivel[] = [
        {valor: 'Básico'},
        {valor: 'Medio'},
        {valor: 'Avanzado'},
        {valor: 'Superior'},
      ];
      niveles_RA: Nivel[];
      niveles_Comp: Nivel[];
    
    
      norma : Norma;
      idNorma : number;
    
      taxonomia: Taxonomia;
    
      cursos : Curso[];
    
      eje : Eje_curricular;
    
      carrera : Carrera;
    
      resultados : Resultado_aprendizaje[];
    
      competencias : Competencia[];
    
      getCursos(): void{
        this.CursoService
          .getCurso()
          .then(
            cursos => {
              if (cursos.length == 0){
                console.log('No hay cursos');
              }
              else{
                console.log(cursos);
                this.cursos = cursos
              }
            }
          );
      }
    
      getCursosByIDEje(idEje): void{
        this.CursoService
        .getCursoByIDEje(idEje)
        .then(
          cursos => {
            if (cursos.length == 0){
              console.log('No hay cursos');
            }
            else{
              console.log(cursos);
              this.cursos = cursos
            }
          }
        );
      }
    
      getEjeByID(idEje){
        this.EjeCurricularService
          .getEjeByID(idEje)
          .then(
            eje => {
              if (!eje){
                console.log('No hay eje');
              }
              else{
                console.log(eje);
                this.eje = eje;
              }
            }
          );
      }
    
      getCarreraByID(idCarrera){
        this.CarreraService
          .getCarreraByID(idCarrera)
          .then(
            carrera => {
              if (!carrera){
                console.log('No hay carrera');
              }
              else{
                console.log(carrera);
                this.carrera = carrera;
              }
            }
          );
      }
    
      deleteCurso(cur): void{
        this.CursoService.deleteCurso(cur.id);
        this.cursos = this.cursos.filter(c => c !== cur);
      }
    
      /* 
      Por ahora retorna todos los resultados, despues debe retornar 
      los correspondientes al curso seleccionado 
      */
      getResultadosAprendizajeByIDcurso(){
        this.ResultadoAprendizajeService
          .getResultadoAprendizaje()
          .then(
            resultados => {
              if (resultados.length == 0){
                console.log('No hay resultados');
              }
              else{
                console.log(resultados);
                this.resultados = resultados
              }
            }
          );
      }
    
    
      getNormaActiva(){
        this.NormaService
          .getNormaActiva()
          .then(
            norma => {
              if (!norma){
                console.log('No hay norma');
              }
              else{
                console.log(norma);//funciona
                this.norma = norma;
              }
            } 
            // return 
          );
        // return this.norma;
      }
    
    
    
      getCompetenciasByIdNorma(id){
        this.CompetenciaService
        .getCompetenciasByIdNorma(id)
        .then(
          competencias => {
            if (competencias.length == 0){
              console.log('No hay competencias');
            }
            else{
              console.log(competencias);
              this.competencias = competencias;
            }
          }
        );
      }
    
      getResultadosByEje(idEje){}
    
      getTaxonomiaDeResultadosAprendizaje(idTaxonomia){
        this.TaxonomiaService
        .getTaxonomiaByID(idTaxonomia)
        .then(
          taxonomia => {
            if (!taxonomia){
              console.log('No hay taxonomia');
            }
            else{
              console.log(taxonomia);
              this.taxonomia = taxonomia;
            }
          }
        );
      }
    
      getNivelesByTaxonomia(idTax){
        // obtener niveles de la taxonomia asociada a los resultados de aprendizaje asociados al eje curricular
        /*
          1.-getResultados del eje
          2.-getTaxonomia del resultado de aprendizaje
          3.-getNiveles de la taxonomia
        */
      }
    
      getNivelesCompetencias(idNorma){
        // obtener niveles de la taxonomia asociada a la norma
      }
    
      goToUnidades(){
        this.router.navigate(['/unidades/constructor-de-cursos']);
      }
    
      constructor(
        private CursoService: CursoService,
        private EjeCurricularService: EjeCurricularService,
        private CarreraService: CarreraService,
        private ResultadoAprendizajeService: ResultadoAprendizajeService,
        private NormaService: NormaService,
        private CompetenciaService: CompetenciaService,
        private TaxonomiaService: TaxonomiaService,
        private route: ActivatedRoute,
        private router : Router,
        ) { }
    
      ngOnInit() {
        this.getNormaActiva();
        let idEje = this.route.snapshot.paramMap.get('idEje');
        let idCarrera = this.route.snapshot.paramMap.get('idCarrera');
        this.getCursosByIDEje(idEje);
        this.getEjeByID(idEje);
        this.getCarreraByID(idCarrera);
        this.getResultadosAprendizajeByIDcurso();
        this.getCompetenciasByIdNorma(1); 
    
      }
    
    }
    

    Any suggestions? Thank you PS: I'm studying async and promises

        
    asked by Oscar Acevedo Osses 15.12.2018 в 17:38
    source

    0 answers