/yacs/static/global/js/v1/models.js

https://github.com/jeffh/YACS · JavaScript · 179 lines · 166 code · 12 blank · 1 comment · 7 complexity · 4757e5c252828a0f9381d11d9ad3138c MD5 · raw file

  1. (function(Backbone, _, window, undefined){
  2. function readOnlySync(method, model, options){
  3. if (_.include(['create', 'update', 'delete'], method))
  4. return;
  5. return Backbone.sync(method, model, options);
  6. }
  7. function api(){
  8. return '/api/v3/' + Array.prototype.join.call(arguments, '/') + '/';
  9. }
  10. function parseJSONReponse(json){
  11. if (json.status === 'OK'){
  12. return json.payload;
  13. } else {
  14. console.log('JSON request failed: ', json);
  15. }
  16. }
  17. var ModelBase = Backbone.Model.extend({
  18. initialize: function(attributes){
  19. var self = this;
  20. _.each(attributes, function(value, name){
  21. var klass = window[(self.collectionFields || {})[name]];
  22. if (klass){
  23. obj = {};
  24. obj[name] = self._collectionize(value, klass);
  25. self.set(obj, {slient: true});
  26. }
  27. });
  28. },
  29. _collectionize: function(json, collectionClass){
  30. var collection = new collectionClass();
  31. _.each(json, function(item){
  32. collection.create(item);
  33. });
  34. return collection;
  35. },
  36. collectionFields: null,
  37. sync: readOnlySync,
  38. jsonifyFields: [],
  39. toJSON: function(){
  40. var data = Backbone.Model.prototype.toJSON.call(this);
  41. data.url = this.url().substr('/api/v3'.length);
  42. var self = this;
  43. _.each(this.jsonifyFields || [], function(name){
  44. data[name] = self.get(name).toJSON();
  45. });
  46. return data;
  47. },
  48. parse: parseJSONReponse
  49. });
  50. var CollectionBase = Backbone.Collection.extend({
  51. semesterID: null,
  52. getSemesterQueryString: function(){
  53. if (!this.get('semesterID')){
  54. return '';
  55. }
  56. return 'semester=' + this.get('semesterID');
  57. },
  58. parse: parseJSONReponse
  59. });
  60. var Semester = ModelBase.extend({
  61. url: function(){
  62. return api('semesters', this.id);
  63. }
  64. });
  65. var Department = ModelBase.extend({
  66. url: function(){
  67. var sem = this.get('semester');
  68. return api('departments', this.get('code'));
  69. }
  70. });
  71. var Course = ModelBase.extend({
  72. defaults: {
  73. marked: false,
  74. sections: null
  75. },
  76. url: function(){
  77. var sem = this.get('semester');
  78. return api('courses', this.id);
  79. },
  80. jsonifyFields: ['semester'],
  81. mark: function(){
  82. _.each(this.get('sections'), function(section){
  83. section.mark();
  84. });
  85. this.set({marked: true});
  86. console.log('course marked ' + this.get('name'));
  87. },
  88. unmark: function(){
  89. _.each(this.get('sections'), function(section){
  90. section.unmark();
  91. });
  92. this.set({marked: false});
  93. console.log('course unmarked ' + this.get('name'));
  94. }
  95. });
  96. var Section = ModelBase.extend({
  97. defaults: {
  98. marked: false
  99. },
  100. idAttribute: 'crn',
  101. collectionFields: {periods: 'PeriodList'},
  102. jsonifyFields: ['course', 'semester'],
  103. url: function(){
  104. var sem = this.get('semester');
  105. return api('sections', this.get('crn'));
  106. },
  107. mark: function(){
  108. this.set({marked: true});
  109. console.log('section marked ' + this.get('crn'));
  110. },
  111. unmark: function(){
  112. this.set({marked: false});
  113. console.log('section unmarked ' + this.get('crn'));
  114. }
  115. });
  116. var Period = ModelBase.extend({
  117. url: function(){
  118. return api('periods', this.id);
  119. }
  120. });
  121. var Semesters = ModelBase.extend({
  122. model: Semester,
  123. comparator: function(semester){
  124. return semester.get('year') * 12 + semester.get('month');
  125. },
  126. url: function(){
  127. return api('semesters');
  128. }
  129. });
  130. var Departments = CollectionBase.extend({
  131. model: Department,
  132. url: function(){
  133. return api('departments') + '?' + this.getSemesterQueryString();
  134. }
  135. });
  136. var Courses = CollectionBase.extend({
  137. model: Course,
  138. url: function(){
  139. return api('courses') + '?' + this.getSemesterQueryString();
  140. }
  141. });
  142. var Sections = CollectionBase.extend({
  143. model: Section,
  144. url: function(){
  145. return api('sections') + '?' + this.getSemesterQueryString();
  146. }
  147. });
  148. var Periods = CollectionBase.extend({
  149. model: Period,
  150. url: function(){
  151. return api('periods') + '?' + this.getSemesterQueryString();
  152. }
  153. });
  154. // export
  155. _.extend(window, {
  156. Semester: Semester,
  157. Department: Department,
  158. Course: Course,
  159. Section: Section,
  160. Period: Period,
  161. SemesterList: Semesters,
  162. DepartmentList: Departments,
  163. CourseList: Courses,
  164. SectionList: Sections,
  165. PeriodList: Periods
  166. })
  167. })(Backbone, _, window);