Backbone.js以某种方式过滤集合

时间:2014-01-30 23:34:25

标签: javascript backbone.js filter underscore.js

我的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,但我想知道我可以改进这段代码吗? 流程看起来像这样

  1. 随机选择 X 正确答案
  2. 随机选择 Y 错误答案
  3. 随机播放所有答案
  4. 创建可以传递给新集合
  5. 的答案数组

    这段代码看起来并不酷,也许你会有一些想法如何使它更酷?:)

0 个答案:

没有答案