Ordering.js 907 Bytes
define(function(require) {
    'use strict';
    var Backbone = require('backbone');

    var Ordering = Backbone.Model.extend({
        defaults: function defaults() {
            return {
                value: null,
                items: new Backbone.Collection(),
            };
        },
        initialize: function(data, options) {
            if (this.get('value') === null) {
                var firstItem = this.get('items').at(0);
                if (firstItem) {
                    this.set('value', firstItem.get('value'));
                }
            }
        },
        parse: function(data) {
            var result = _.clone(data);
            if (result.items && !(result.items instanceof Backbone.Collection)) {
                result.items = new Backbone.Collection(result.items, {parse: true});
            }
            return result;
        },
    });
    return Ordering;
});