簡體   English   中英

如何在 Ember.js 中執行順序 for 循環?

[英]How to do a sequential for loop in Ember.js?

我正在使用 ember 2.1,我會為使用 Javascript FileReader api 獲得的文本文件的每一行應用一個操作。 有人知道該怎么做嗎?

-當我嘗試使用“foreach”時,執行不是順序的:程序在開始處理下一行之前不會等待記錄一行; 因此我跳過了行並且無法控制執行;
- 我嘗試了一個簡單的 for 循環,但它不起作用,我讀到它們在 Ember 操作中是不可能的;
- 當我使用對 Ember 操作的遞歸調用(在以下代碼中: saveNew(lines) ,首先由openFile(event)調用)時,會發生另一個問題:每個新行都會替換最后一行而不是添加到最后一行,最后只記錄最后一行。


  saveNew(lines) {
    const promise1 = new Promise((resolve, reject) => {

      var line = lines.shift();
      var cols = line.split(';');
      var c = 0;
      var patt = /[A-z ]/g;
      var result = patt.test(line);
      if (result == true) {

        this.model.name = cols[0];
        console.log("named");
        this.model.url = cols[1];
        console.log("urled");
        this.model.description = cols[2];
        console.log("descripted");
        console.log(cols[2]);
        this.get('model').save().then((source) => {
          console.log("in the insertion");

          this.send('toast', 'Source créée.');
          console.log("wooouu");
          this.transitionToRoute('signed-in.sources.source', source);
          console.log("incredible");

        }).then(() => {
          setTimeout(() => {
            resolve(lines);
          }, 1000);
        });
      } else {
        resolve(lines);
      }
    });

    promise1.then((value) => {
      if (value.length > 0) {
        this.send('saveNew', value);
      }
    });
  },

  openFile(event) {
    console.log('upload');
    var input = event.target;
    var reader = new FileReader();
    console.log('ici');

    reader.addEventListener("load", (function(e) {
      var text = e.target.result;
      text.replace(/(\r\n)|\r|\n/g, '\n'); //normalisation des caractères de retour à la ligne
      var lines = text.split(/\n+/g); //séparation du texte du fichier en lignes
      var insnumb = 0;
      const taille = lines.length;
      this.send('saveNew', lines);
    }).bind(this));
    reader.readAsText(input.files[0]);
  }
}

使用async/await可以幫助承諾並按順序運行內容

不確定這是否有幫助:

var e = {
  async saveNew (lines) {
    const pattern = /[A-z ]/g
    
    for (const line of lines) {
      if (pattern.test(line)) {
        const cols = line.split(';')
        
        this.model.name = cols[0]
        this.model.url = cols[1]
        this.model.description = cols[2]
        
        const source = await this.get('model').save()
        this.send('toast', 'Source créée.')
        this.transitionToRoute('signed-in.sources.source', source)
      }
    }
  },
  
  async openFile (event) {
    const [file] = event.target.files
    if (!file) return
    const text = await file.text()
    // Normalisation des caractères de retour à la ligne
    text.replace(/(\r\n)|\r|\n/g, '\n') 
    // Séparation du texte du fichier en lignes
    const lines = text.split(/\n+/g)
    this.send('saveNew', lines)
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM