I've got this Collection in my Backbone application:
var AnswersCollection = Backbone.Collection.extend({
model: Answer,
initialize: function() {
console.log('Hello from new answers collection');
},
getCorrect: function() {
return this.where({correct: true});
},
getWrong: function() {
return this.where({correct: false});
},
randomSet: function(correct, wrong) {
arr1 = _.sample(this.getCorrect(), correct);
arr2 = _.sample(this.getWrong(), wrong);
result = _.shuffle(arr1.concat(arr2));
coll = new AnswersCollection(result)
return coll;
}
});
This works, and randomSet is called by a View when config have certain options or user click on 'rerender' button, but I'm wondering that maybe I can improve this code ? The flow looks like this
- Get randomly selected X correct answers
- Get randomly selected Y wrong answers
- Shuffle all answers
- Create array of answers, that can be passed to new Collection
This code doesn't look to cool, maybe you will have some ideas how to make It cooler?:)