group.js 1.29 KB
Newer Older
1
define([
2 3 4
    'backbone', 'underscore', 'underscore.string', 'gettext',
    'backbone.associations'
], function(Backbone, _, str, gettext) {
5 6 7
    'use strict';
    var Group = Backbone.AssociatedModel.extend({
        defaults: function() {
8 9
            return {
                name: '',
10
                version: 1,
11 12 13 14
                order: null,
                usage: []
            };
        },
15
        url: function() {
16 17 18 19 20
            var parentModel = this.collection.parents[0];
            return parentModel.urlRoot + '/' + encodeURIComponent(parentModel.id) + '/' + encodeURIComponent(this.id);
        },

        reset: function() {
21
            this.set(this._originalAttributes, {parse: true});
22 23 24 25 26 27 28
        },

        isEmpty: function() {
            return !this.get('name');
        },

        toJSON: function() {
29
            return {
30
                id: this.get('id'),
31
                name: this.get('name'),
32 33
                version: this.get('version'),
                usage: this.get('usage')
34
            };
35 36 37
        },

        validate: function(attrs) {
38
            if (!str.trim(attrs.name)) {
39 40
                return {
                    message: gettext('Group name is required'),
41
                    attributes: {name: true}
42 43 44 45 46 47 48
                };
            }
        }
    });

    return Group;
});