PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/static-data/static/js/models.js

https://bitbucket.org/caret/timetables-2
JavaScript | 166 lines | 123 code | 25 blank | 18 comment | 8 complexity | 69d5580e1f7297a22568d661f04c6316 MD5 | raw file
Possible License(s): Apache-2.0
  1. define(["jquery", "underscore", "backbone", "backbone-utils"],
  2. function($, _, Backbone, backbone_utils) {
  3. "use strict";
  4. var superclass = backbone_utils.superclass;
  5. var superproto = backbone_utils.superproto;
  6. // We should never need to import views.js in models. Models should not know
  7. // about views.
  8. var Organisation = Backbone.Model.extend({
  9. constructor: function Organisation() {
  10. superclass(Organisation).apply(this, arguments);
  11. },
  12. defaults: {
  13. name: undefined,
  14. id: undefined,
  15. code: undefined
  16. }
  17. });
  18. var Organisations = Backbone.Collection.extend({
  19. constructor: function Organisations() {
  20. superclass(Organisations).apply(this, arguments);
  21. },
  22. model: Organisation
  23. });
  24. var Year = Backbone.Model.extend({
  25. constructor: function Year() {
  26. superclass(Year).apply(this, arguments);
  27. },
  28. initialize: function() {
  29. var organisationsJSON = this.get("organisations");
  30. this.set({"organisations":
  31. new Organisations(organisationsJSON)});
  32. },
  33. organisationWithCode: function(code) {
  34. return this.get("organisations").find(function(org) {
  35. return org.code === code;
  36. });
  37. },
  38. toString: function() {
  39. return this.get("name");
  40. }
  41. });
  42. var Years = Backbone.Collection.extend({
  43. constructor: function Years() {
  44. superclass(Years).apply(this, arguments);
  45. },
  46. model: Year
  47. });
  48. /**
  49. * Maintains a Years collection of Year models, plus the index of the
  50. * active year.
  51. */
  52. var YearsState = Backbone.Model.extend({
  53. constructor: function YearsState() {
  54. superclass(YearsState).apply(this, arguments);
  55. },
  56. defaults: {
  57. selectedYear: 0,
  58. selectedOrganisation: 0,
  59. years: null
  60. },
  61. initialize: function() {
  62. // var years = this.get("years");
  63. // if(years && years.length > 0) {
  64. // this.set({selectedYear: 0});
  65. // var orgs = years.get("organisation");
  66. // if()
  67. // }
  68. },
  69. selectYear: function(start_year) {
  70. // Find the index of the new year
  71. var index = null;
  72. this.years().each(function(year, i) {
  73. if(year.get("start_year") === start_year) {
  74. index = i;
  75. return {}; // break iteration
  76. }
  77. });
  78. // When updating the year we need to keep track of the current
  79. // organisation and try to re-select an equivalent from the new
  80. // year's organisation list.
  81. var oldOrganisation = this.selectedOrganisation();
  82. var organisationCode = null;
  83. if(oldOrganisation) {
  84. organisationCode = oldOrganisation.get("code");
  85. }
  86. // Update the selected year and organisation
  87. this.set({ selectedYear: index || 0 });
  88. this.selectOrganisation(organisationCode);
  89. return this;
  90. },
  91. selectOrganisation: function(organisationCode) {
  92. var index = null;
  93. this.organisations().each(function(org, i) {
  94. if(org.get("code") === organisationCode) {
  95. index = i;
  96. return {}; // break iteration
  97. }
  98. });
  99. this.set({ selectedOrganisation: index });
  100. return this;
  101. },
  102. selectedYear: function() {
  103. var selectedYear = this.get("selectedYear");
  104. if(this.years() && this.years().length) {
  105. return this.years().at(this.get("selectedYear"));
  106. }
  107. return null;
  108. },
  109. selectedOrganisation: function() {
  110. var orgs = this.organisations();
  111. if(!orgs) {
  112. return null;
  113. }
  114. return orgs.at(this.get("selectedOrganisation"));
  115. },
  116. years: function() {
  117. return this.get("years");
  118. },
  119. organisations: function() {
  120. var year = this.selectedYear();
  121. if(!year) {
  122. return null;
  123. }
  124. return year.get("organisations");
  125. }
  126. },
  127. // Class properties
  128. {
  129. fromJSON: function(data) {
  130. var years = new Years(data);
  131. return new YearsState({years: years});
  132. }
  133. });
  134. return {
  135. Organisation: Organisation,
  136. Organisations: Organisations,
  137. Year: Year,
  138. Years: Years,
  139. YearsState: YearsState
  140. };
  141. });