component_template.js 1.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Simple model for adding a component of a given type (for example, "video" or "html").
 */
define(["backbone"], function (Backbone) {
    return Backbone.Model.extend({
        defaults: {
            type: "",
            // Each entry in the template array is an Object with the following keys:
            // display_name
            // category (may or may not match "type")
            // boilerplate_name (may be null)
            // is_common (only used for problems)
            templates: []
        },
        parse: function (response) {
16 17 18 19 20 21 22 23
            // Returns true only for templates that both have no boilerplate and are of
            // the overall type of the menu. This allows other component types to be added
            // and they will get sorted alphabetically rather than just at the top.
            // e.g. The ORA openassessment xblock is listed as an advanced problem.
            var isPrimaryBlankTemplate = function(template) {
                return !template.boilerplate_name && template.category === response.type;
            };

24 25
            this.type = response.type;
            this.templates = response.templates;
26
            this.display_name = response.display_name;
27 28 29

            // Sort the templates.
            this.templates.sort(function (a, b) {
30 31 32 33 34 35 36 37
                // The blank problem for the current type goes first
                if (isPrimaryBlankTemplate(a)) {
                    return -1;
                } else if (isPrimaryBlankTemplate(b)) {
                    return 1;
                } else if (a.display_name > b.display_name) {
                    return 1;
                } else if (a.display_name < b.display_name) {
38 39
                    return -1;
                }
40
                return 0;
41 42 43 44
            });
        }
    });
});