0
我想在我的集合的每個模型中設置屬性,但我使用的代碼不工作(屬性尚未更新),儘管它當我直接運行到控制檯時工作。這裏是我的代碼,有問題的代碼是questions.forEach ...:model.set在控制檯中但在我的實際腳本中不知道
var Question = Backbone.Model.extend({
urlRoot: "questions.json"
});
var Questions = Backbone.Collection.extend({
model: Question,
url: "questions.json",
parse: function(response){
return response.questions;
}
});
var questions = new Questions();
questions.fetch();
var questionOrder = [];
while (questionOrder.length != 10){
var randomNum = Math.floor(Math.random() * 10);
if (questionOrder.indexOf(randomNum) == -1) {
questionOrder.push(randomNum);
}
}
questions.forEach(function(model, i) {
model.set({"order": questionOrder[i]});
});