chapter.js 1.89 KB
Newer Older
1
define(['backbone', 'gettext', 'backbone.associations'], function(Backbone, gettext) {
2 3 4
    var Chapter = Backbone.AssociatedModel.extend({
        defaults: function() {
            return {
5 6
                name: '',
                asset_path: '',
7 8 9 10 11 12 13
                order: this.collection ? this.collection.nextOrder() : 1
            };
        },
        isEmpty: function() {
            return !this.get('name') && !this.get('asset_path');
        },
        parse: function(response) {
14
            if ('title' in response && !('name' in response)) {
15 16 17
                response.name = response.title;
                delete response.title;
            }
18
            if ('url' in response && !('asset_path' in response)) {
19 20 21 22 23 24 25 26 27 28 29 30 31 32
                response.asset_path = response.url;
                delete response.url;
            }
            return response;
        },
        toJSON: function() {
            return {
                title: this.get('name'),
                url: this.get('asset_path')
            };
        },
        // NOTE: validation functions should return non-internationalized error
        // messages. The messages will be passed through gettext in the template.
        validate: function(attrs, options) {
33
            if (!attrs.name && !attrs.asset_path) {
34
                return {
35
                    message: gettext('Chapter name and asset_path are both required'),
36 37
                    attributes: {name: true, asset_path: true}
                };
38
            } else if (!attrs.name) {
39
                return {
40
                    message: gettext('Chapter name is required'),
41 42 43 44
                    attributes: {name: true}
                };
            } else if (!attrs.asset_path) {
                return {
45
                    message: gettext('asset_path is required'),
46 47 48 49 50 51 52
                    attributes: {asset_path: true}
                };
            }
        }
    });
    return Chapter;
});