dimanche 19 avril 2015

model.set woking in the console but not in my actual script

I am trying to set an attribute in each of the models of my collection but the code I am using is not working (attributes have not been updated), though it works when I run it directly into the console. Here is my code, the code in question is the questions.forEach...:



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]});

});

Aucun commentaire:

Enregistrer un commentaire