我的Backbone应用程序中有这个Collection:
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;
}
});
这有效,当配置有某些选项或用户点击'rerender'按钮时,由View调用randomSet,但我想知道我可以改进这段代码吗? 流程看起来像这样
这段代码看起来并不酷,也许你会有一些想法如何使它更酷?:)