PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/templates/public/scripts/models/Station.js

https://bitbucket.org/taxilian/racecontrol5
JavaScript | 58 lines | 54 code | 4 blank | 0 comment | 5 complexity | c0968e42b833f9050e235c3db7cdbfe7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. define(["backbone", "underscore", "jquery", "defaults", "datelib"], function(Backbone, _, $, defaults) {
  2. var Stations, Station = Backbone.Model.extend({
  3. idAttribute: "stationNumber",
  4. url: function() {
  5. var url = "/newAPI/events/" + this.get("raceEvent_id") + "/stations";
  6. if (!this.isNew()) {
  7. url += "/" + this.id;
  8. }
  9. return url;
  10. },
  11. validate: function(attrs) {
  12. var self = this;
  13. var col = this.collection;
  14. if ("name" in attrs && !attrs.name) {
  15. return "Please enter a station name";
  16. }
  17. }
  18. }, {
  19. fetchById: function(eventId, sId) {
  20. return $.when(Stations.fetchAll(eventId)).pipe(function(stations) {
  21. return stations.get(sId);
  22. });
  23. }
  24. });
  25. Stations = Backbone.Collection.extend({
  26. model: Station,
  27. url: function() {
  28. return "/newAPI/events/" + this.eventId + "/stations";
  29. },
  30. comparator: function(station) {
  31. return station.id;
  32. }
  33. }, {
  34. list: null,
  35. fetchAll: function(eventId) {
  36. var self = this, cache;
  37. if (!self.list) { self.list = {}; }
  38. cache = self.list[eventId];
  39. if (cache) {
  40. return cache;
  41. }
  42. var tmp = new self();
  43. tmp.eventId = eventId;
  44. cache = self.list[eventId] = tmp.fetch().pipe(function() {
  45. return (self.list[eventId] = tmp);
  46. });
  47. return cache;
  48. }
  49. });
  50. return {
  51. obj: Station,
  52. list: Stations
  53. };
  54. });