Javascript 如何在Ember.js中执行顺序for循环?
我使用的是ember 2.1,我想对使用Javascript FileReader api获得的文本文件的每一行应用一个操作。有人知道怎么做吗Javascript 如何在Ember.js中执行顺序for循环?,javascript,ember.js,Javascript,Ember.js,我使用的是ember 2.1,我想对使用Javascript FileReader api获得的文本文件的每一行应用一个操作。有人知道怎么做吗 -当我尝试使用“foreach”时,执行是不连续的:程序在开始处理下一行之前不会等待一行被记录下来;因此,我跳过了行,无法控制执行 -我尝试了一个简单的for循环,但它不起作用,我读到它们在余烬操作中是不可能的 -当我使用对Ember操作的递归调用(在下面的代码中:saveNew(lines),首先由openFile(event)调用)时,会出现另一个问
-当我尝试使用“foreach”时,执行是不连续的:程序在开始处理下一行之前不会等待一行被记录下来;因此,我跳过了行,无法控制执行
-我尝试了一个简单的for循环,但它不起作用,我读到它们在余烬操作中是不可能的
-当我使用对Ember操作的递归调用(在下面的代码中:saveNew(lines),首先由openFile(event)调用)时,会出现另一个问题:每一个新行都会替换最后一行,而不是添加到最后一行,最后只记录最后一行
使用可以帮助实现承诺并按顺序运行 不确定这是否有帮助:
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)
}
}
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)
}
}