persistenceJS ( http://persistencejs.org )を使用して、データベース値の 1 つに for ループを使用してデータベースにデータを入力しようとしています。
私が抱えている問題は、alert(i) を実行してから persistenceJS 呼び出しを実行すると、persistenceJS が実行される前に両方のアラートが表示されることです。
以下のコードはもう少しよく説明しています
for(var i = 0; i < 2; i++) {
alert(i); //0 and 1 are alerted then the below runs
Answer.all().count(function(co) {
alert('current count is:' + co); //alerts 0
});
}
上は問題を示すテスト コードで、下は実際に使用するコードです。
Answer = persistence.define('Answer', {
level: "INT",
image: "INT",
correct: "INT",
hints: "INT"
});
persistence.store.websql.config(persistence,'FootballQuiz', 'Storage of answer status', 5 * 1024 * 1024);
persistence.schemaSync();
for(var i = 0; i < 2; i++)
//checks to see if the current value exists in the db
Answer.all().filter('level', '=', 1)
.and(new persistence.PropertyFilter('image', '=', i))
.count(function(c){
if(c > 0) {
alert('already exists');
} else {
var tmpAnswer = new Answer();
tmpAnswer.level = "1";
tmpAnswer.image = i;
tmpAnswer.correct = "0";
persistence.add(tmpAnswer);
persistence.flush();
}
});
}
persistenceJS で各ループを実行したいと思います。ありがとう