PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

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

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