无法从承诺中填充列表并在 html - aurelia.

Can't get list filled from a promise and use it in html - aurelia

本文关键字:html aurelia 列表 填充 承诺      更新时间:2023-09-26

我无法填充 finalList 以在我的 html 文件中使用,它将在承诺所有代码之前运行代码来填充它。我需要在我的 html 文档中使用这个数组,所以它必须是我正在使用 Aurelia。

activate() {
    var repoList = [];
    var repos = this.http.fetch({Link to github api})
                .then(response => response.json())
                .then(repos => this.repos = repos);
    var trello = new Trello;
    trello.getBoards().then(boardList => this.boards = boardList);
    var boards = trello.getBoards();
    //add github repo to the associated trello board (works)
    Promise.all([boards, repos]).then(function(values) {
        var count = 0;
        for (var i in values[0]) {
            for (var a in values[1]) {
                if (values[1][a].hasOwnProperty("name")) {
                    var repo = values[1][a].name.toLowerCase();
                    var board = values[0][i]['name'].toLowerCase();
                    repoList[count] = repo;
                    count++;
                    if (repo == board) {
                        console.log(repo + " " + board)
                    }
                }
           }
      }      
   });
   //this list is always empty (The problem)
   this.finalList = repoList;
   this.title = "Trello Boards";
}

这样的事情应该可以做到。很难破译 for 循环中发生了什么。

activate() {
  let reposPromise = this.http.fetch({Link to github api})
    .then(response => response.json());
  let boardsPromise = new Trello().getBoards();
  return Promise.all([boardsPromise, reposPromise])
    .then(([boards, repos]) => {
      this.boards = boards;
      this.repos = repos;
      this.finalList = [];
      for (var i in boards) {
        for (var a in repos) {
          if (values[1][a].hasOwnProperty("name")) {
            var repo = values[1][a].name.toLowerCase();
            var board = values[0][i]['name'].toLowerCase();
            this.finalList.push(repo);
            if (repo == board) 
            {
              console.log(repo + " " + board)
            }
          }
        }
      }      
    });
  this.title = "Trello Boards";
}

我相信你的finalList应该在承诺处理程序中设置。喜欢这个。

activate() {
  var repoList = [];
  //I always use this, and I am not sure what do you mean
  //by this.finalList, but still I assume you know what you are doing
  //And hence I use this!
  var that = this;
  var repos = this.http.fetch({Link to github api})
  .then(response => response.json())
  .then(repos => this.repos = repos);
  var trello = new Trello;
  trello.getBoards().then(boardList => this.boards = boardList);
  var boards = trello.getBoards();
  //add github repo to the associated trello board (works)
  Promise.all([boards, repos]).then(function(values) {
    var count = 0;
    for (var i in values[0]) {
      for (var a in values[1]) {
        if (values[1][a].hasOwnProperty("name"))
        {
          var repo = values[1][a].name.toLowerCase();
          var board = values[0][i]['name'].toLowerCase();
          repoList[count] = repo;
          count++;
          if (repo == board) 
          {
            console.log(repo + " " + board)
          };
        }
      };
    };      
     //I believe when promise resolves. You should set the  repoList.
     that.finalList = repoList;
     that.title = "Trello Boards";
  });
}

我的问题是,你真的想把标题和最终列表设置为this吗?只是问。

希望这有帮助!