CategoryResetter

by david_i_smith

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
<div class="btn-group" data-category="psat">
  <input checked='checked' type='radio' data-report-selector='{"assessmentVersion": 3}'>
</div>
<div class="btn-group" data-category="sat">
  <input checked='checked' type='radio' data-report-selector='{"assessmentVersion": 3}'>
</div>

JavaScript

var CategoryResetter = function (options) {
    this.$el = options.$el;
    this.model = options.model;
    this.config = options.config;
    this.setData();
};

CategoryResetter.prototype.setData = function () {
    this.data = JSON.parse(this.$el.attr('data-report-selector'));
};

CategoryResetter.prototype.reset = function () {
    if (this.$el.is(':checked') && this.data.hasOwnProperty('assessmentVersion')) {
        this.update(this.data.assessmentVersion);
    } else {
        throw new Error('fucknuts');
    }
};

CategoryResetter.prototype.set = function (supportsHistorical) {
    if (supportsHistorical) {
        this.update(3);
    } else {
        this.update(1);
    }
};

CategoryResetter.prototype.update = function (assessmentVersion) {
    var modelAttr = this.model.attributes.p_asmt_id;
    var assessmentIDs = this.config.assessmentIDs;
    var callbacks = this.config.callbacks;
    var viewValue;
    var modelValue;
    if (assessmentVersion === 1) {
        viewValue = assessmentIDs.current[0];
        modelValue = this.getCurrentAssessmentID();
        if (callbacks[0]) {
            callbacks[0].apply(this);
        }
    }
    if (assessmentVersion === 2) {
        viewValue = assessmentIDs.historical;
        modelValue = assessmentIDs.historical;
        if (callbacks[1]) {
            callbacks[1].apply(this);
        }
    }
    if (assessmentVersion === 3) {
        viewValue = assessmentIDs.current[0] + ',' + assessmentIDs.historical;
        modelValue = this.getCurrentAssessmentID() + ',' + assessmentIDs.historical;
        if (callbacks[2]) {
            callbacks[2].apply(this);
        }
    }
    this.$el.val(viewValue);
    modelAttr = modelValue;
    console.log(viewValue);
    console.log(modelValue);
    console.log(this.model.get('p_scr_tier'));
};

CategoryResetter.prototype.getCurrentAssessmentID = function () {
    var currentAssessmentIDs = this.config.assessmentIDs.current;
    var modelValue =...