certificate.js 3.85 KB
Newer Older
1 2
// Backbone.js Application Model: Certificate

3
define([
4 5 6 7 8 9 10 11 12
    'underscore',
    'backbone',
    'backbone-relational',
    'backbone.associations',
    'gettext',
    'cms/js/main',
    'js/certificates/models/signatory',
    'js/certificates/collections/signatories'
],
13 14 15 16 17 18 19 20
    function(_, Backbone, BackboneRelational, BackboneAssociations, gettext, CoffeeSrcMain,
             SignatoryModel, SignatoryCollection) {
        'use strict';
        var Certificate = Backbone.RelationalModel.extend({
            idAttribute: 'id',
            defaults: {
                // Metadata fields currently displayed in web forms
                course_title: '',
21

22 23 24
                // Metadata fields not currently displayed in web forms
                name: 'Name of the certificate',
                description: 'Description of the certificate',
25

26 27 28 29
                // Internal-use only, not displayed in web forms
                version: 1,
                is_active: false
            },
30

31 32 33 34 35 36 37 38 39 40 41
            // Certificate child collection/model mappings (backbone-relational)
            relations: [{
                type: Backbone.HasMany,
                key: 'signatories',
                relatedModel: SignatoryModel,
                collectionType: SignatoryCollection,
                reverseRelation: {
                    key: 'certificate',
                    includeInJSON: 'id'
                }
            }],
42

43 44 45 46 47 48 49 50 51 52
            initialize: function(attributes, options) {
                // Set up the initial state of the attributes set for this model instance
                this.canBeEmpty = options && options.canBeEmpty;
                if (options.add) {
                    // Ensure at least one child Signatory model is defined for any new Certificate model
                    attributes.signatories = new SignatoryModel({certificate: this});
                }
                this.setOriginalAttributes();
                return this;
            },
53

54 55 56 57
            parse: function(response) {
                // Parse must be defined for the model, but does not need to do anything special right now
                return response;
            },
58

59 60 61
            setOriginalAttributes: function() {
                // Remember the current state of this model (enables edit->cancel use cases)
                this._originalAttributes = this.parse(this.toJSON());
62

63 64 65
                this.get('signatories').each(function(modelSignatory) {
                    modelSignatory.setOriginalAttributes();
                });
asadiqbal committed
66

67 68 69 70 71
                // If no url is defined for the signatories child collection we'll need to create that here as well
                if (!this.isNew() && !this.get('signatories').url) {
                    this.get('signatories').url = this.collection.url + '/' + this.get('id') + '/signatories';
                }
            },
72

73 74 75 76 77 78 79 80
            validate: function(attrs) {
                // Ensure the provided attributes set meets our expectations for format, type, etc.
                if (!attrs.name.trim()) {
                    return {
                        message: gettext('Certificate name is required.'),
                        attributes: {name: true}
                    };
                }
81
                var allSignatoriesValid = _.every(attrs.signatories.models, function(signatory) {
82 83 84 85 86 87 88 89 90
                    return signatory.isValid();
                });
                if (!allSignatoriesValid) {
                    return {
                        message: gettext('Signatory field(s) has invalid data.'),
                        attributes: {signatories: attrs.signatories.models}
                    };
                }
            },
91

92 93 94 95 96 97
            reset: function() {
                // Revert the attributes of this model instance back to initial state
                this.set(this._originalAttributes, {parse: true, validate: true});
            }
        });
        return Certificate;
98
    });