PageRenderTime 29ms CodeModel.GetById 2ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/python/slavealloc/www/js/models.js

https://github.com/rail/build-tools
JavaScript | 258 lines | 195 code | 40 blank | 23 comment | 1 complexity | c9755a0ccdbca6cddf16dc015487ffb5 MD5 | raw file
  1//
  2// Base Classes
  3//
  4
  5var DenormalizedModel = Backbone.Model.extend({
  6    // call this to update denormalized columns (e.g., 'datacenter') to
  7    // their normalized column ('dcid') changes.
  8    bindDenormalizedColumns: function(column_info) {
  9        var self = this;
 10
 11        $.each(column_info, function (i, info) {
 12            var foreign_name_col = info.foreign_name_col || 'name';
 13            var change_fn = function (model, id) {
 14                var newname = (id === null)? null :
 15                        info.model.get(id).get(foreign_name_col);
 16                var set = {};
 17                set[info.name_col] = newname;
 18                model.set(set);
 19            };
 20
 21            self.bind('change:' + info.id_col, change_fn);
 22        });
 23    }
 24});
 25
 26//
 27// Slaves
 28//
 29
 30var Slave = DenormalizedModel.extend({
 31    initialize: function() {
 32        this.id = this.get('slaveid');
 33
 34        this.bindDenormalizedColumns([
 35            { name_col: 'distro', model: window.distros, id_col: 'distroid' },
 36            { name_col: 'datacenter', model: window.datacenters, id_col: 'dcid' },
 37            { name_col: 'bitlength', model: window.bitlengths, id_col: 'bitsid' },
 38            { name_col: 'speed', model: window.speeds, id_col: 'speedid' },
 39            { name_col: 'purpose', model: window.purposes, id_col: 'purposeid' },
 40            { name_col: 'trustlevel', model: window.trustlevels, id_col: 'trustid' },
 41            { name_col: 'environment', model: window.environments, id_col: 'envid' },
 42            { name_col: 'custom_template', model: window.tac_templates,
 43                    id_col: 'custom_tplid', foreign_name_col: 'tplid' },
 44            { name_col: 'pool', model: window.pools, id_col: 'poolid' },
 45            { name_col: 'locked_master', model: window.masters,
 46                    id_col: 'locked_masterid', foreign_name_col: 'nickname' },
 47            { name_col: 'current_master', model: window.masters,
 48                    id_col: 'current_masterid', foreign_name_col: 'nickname' }
 49        ]);
 50    }
 51});
 52
 53var Slaves = Backbone.Collection.extend({
 54    url: window.slavealloc_base_url + 'api/slaves',
 55    model: Slave,
 56    comparator: function(m) {
 57        return m.get('name');
 58    }
 59});
 60
 61//
 62// Masters
 63//
 64
 65var Master = DenormalizedModel.extend({
 66    initialize: function() {
 67        this.id = this.get('masterid');
 68
 69        this.bindDenormalizedColumns([
 70            { name_col: 'pool', model: window.pools, id_col: 'poolid' }
 71        ]);
 72    }
 73});
 74
 75var Masters = Backbone.Collection.extend({
 76    url: window.slavealloc_base_url + 'api/masters',
 77    model: Master,
 78    comparator: function(m) {
 79        return m.get('nickname');
 80    }
 81});
 82
 83//
 84// ID-to-name models
 85//
 86
 87var Distro = Backbone.Model.extend({
 88    initialize: function() {
 89        this.id = this.get('distroid');
 90    }
 91});
 92
 93var Distros = Backbone.Collection.extend({
 94    url: window.slavealloc_base_url + 'api/distros',
 95    model: Distro,
 96
 97    // information about the columns in this collection
 98    columns: [
 99        { id: "name", title: "Name" }
100    ],
101    comparator: function(m) {
102        return m.get('name');
103    }
104});
105
106var Datacenter = Backbone.Model.extend({
107    initialize: function() {
108        this.id = this.get('dcid');
109    }
110});
111
112var Datacenters = Backbone.Collection.extend({
113    url: window.slavealloc_base_url + 'api/datacenters',
114    model: Datacenter,
115
116    // information about the columns in this collection
117    columns: [
118        { id: "name", title: "Name" }
119    ],
120    comparator: function(m) {
121        return m.get('name');
122    }
123});
124
125var Bitlength = Backbone.Model.extend({
126    initialize: function() {
127        this.id = this.get('bitsid');
128    }
129});
130
131var Bitlengths = Backbone.Collection.extend({
132    url: window.slavealloc_base_url + 'api/bitlengths',
133    model: Bitlength,
134
135    // information about the columns in this collection
136    columns: [
137        { id: "name", title: "Name" }
138    ],
139    comparator: function(m) {
140        return m.get('name');
141    }
142});
143
144var Speed = Backbone.Model.extend({
145    initialize: function() {
146        this.id = this.get('speedid');
147    }
148});
149
150var Speeds = Backbone.Collection.extend({
151    url: window.slavealloc_base_url + 'api/speeds',
152    model: Speed,
153
154    // information about the columns in this collection
155    columns: [
156        { id: "name", title: "Name" }
157    ],
158    comparator: function(m) {
159        return m.get('name');
160    }
161});
162
163var Purpose = Backbone.Model.extend({
164    initialize: function() {
165        this.id = this.get('purposeid');
166    }
167});
168
169var Purposes = Backbone.Collection.extend({
170    url: window.slavealloc_base_url + 'api/purposes',
171    model: Purpose,
172
173    // information about the columns in this collection
174    columns: [
175        { id: "name", title: "Name" }
176    ],
177    comparator: function(m) {
178        return m.get('name');
179    }
180});
181
182var Trustlevel = Backbone.Model.extend({
183    initialize: function() {
184        this.id = this.get('trustid');
185    }
186});
187
188var Trustlevels = Backbone.Collection.extend({
189    url: window.slavealloc_base_url + 'api/trustlevels',
190    model: Trustlevel,
191
192    // information about the columns in this collection
193    columns: [
194        { id: "name", title: "Name" }
195    ],
196    comparator: function(m) {
197        return m.get('name');
198    }
199});
200
201var Environment = Backbone.Model.extend({
202    initialize: function() {
203        this.id = this.get('envid');
204    }
205});
206
207var Environments = Backbone.Collection.extend({
208    url: window.slavealloc_base_url + 'api/environments',
209    model: Environment,
210
211    // information about the columns in this collection
212    columns: [
213        { id: "name", title: "Name" }
214    ],
215    comparator: function(m) {
216        return m.get('name');
217    }
218});
219
220var Pool = Backbone.Model.extend({
221    initialize: function() {
222        this.id = this.get('poolid');
223    }
224});
225
226var Pools = Backbone.Collection.extend({
227    url: window.slavealloc_base_url + 'api/pools',
228    model: Pool,
229
230    // information about the columns in this collection
231    columns: [
232        { id: "name", title: "Name" }
233    ],
234    comparator: function(m) {
235        return m.get('name');
236    }
237});
238
239var TACTemplate = Backbone.Model.extend({
240    initialize: function() {
241        this.id = this.get('tplid');
242    }
243});
244
245var TACTemplates = Backbone.Collection.extend({
246    url: window.slavealloc_base_url + 'api/tac_templates',
247    model: TACTemplate,
248
249    // information about the columns in this collection
250    columns: [
251        { id: "name", title: "Name" },
252        { id: "template", title: "Template" }
253    ],
254    comparator: function(m) {
255        return m.get('name');
256    }
257});
258