/public/javascripts/application.js

https://github.com/narangrajeev81/BackboneJsAndFullCalendar · JavaScript · 113 lines · 102 code · 9 blank · 2 comment · 3 complexity · 34daafe8fb4694903bdb090edf54f0c1 MD5 · raw file

  1. $(function(){
  2. var Event = Backbone.Model.extend();
  3. var Events = Backbone.Collection.extend({
  4. model: Event,
  5. url: 'events'
  6. });
  7. var EventsView = Backbone.View.extend({
  8. initialize: function(){
  9. _.bindAll(this);
  10. this.collection.bind('reset', this.addAll);
  11. this.collection.bind('add', this.addOne);
  12. this.collection.bind('change', this.change);
  13. this.collection.bind('destroy', this.destroy);
  14. this.eventView = new EventView();
  15. },
  16. render: function() {
  17. this.el.fullCalendar({
  18. header: {
  19. left: 'prev,next today',
  20. center: 'title',
  21. right: 'month,basicWeek,basicDay'
  22. },
  23. selectable: true,
  24. selectHelper: true,
  25. editable: true,
  26. ignoreTimezone: false,
  27. select: this.select,
  28. eventClick: this.eventClick,
  29. eventDrop: this.eventDropOrResize,
  30. eventResize: this.eventDropOrResize
  31. });
  32. },
  33. addAll: function() {
  34. this.el.fullCalendar('addEventSource', this.collection.toJSON());
  35. },
  36. addOne: function(event) {
  37. this.el.fullCalendar('renderEvent', event.toJSON());
  38. },
  39. select: function(startDate, endDate) {
  40. this.eventView.collection = this.collection;
  41. this.eventView.model = new Event({start: startDate, end: endDate});
  42. this.eventView.render();
  43. },
  44. eventClick: function(fcEvent) {
  45. this.eventView.model = this.collection.get(fcEvent.id);
  46. this.eventView.render();
  47. },
  48. change: function(event) {
  49. // Look up the underlying event in the calendar and update its details from the model
  50. var fcEvent = this.el.fullCalendar('clientEvents', event.get('id'))[0];
  51. fcEvent.title = event.get('title');
  52. fcEvent.color = event.get('color');
  53. this.el.fullCalendar('updateEvent', fcEvent);
  54. },
  55. eventDropOrResize: function(fcEvent) {
  56. // Lookup the model that has the ID of the event and update its attributes
  57. this.collection.get(fcEvent.id).save({start: fcEvent.start, end: fcEvent.end});
  58. },
  59. destroy: function(event) {
  60. this.el.fullCalendar('removeEvents', event.id);
  61. }
  62. });
  63. var EventView = Backbone.View.extend({
  64. el: $('#eventDialog'),
  65. initialize: function() {
  66. _.bindAll(this);
  67. },
  68. render: function() {
  69. var buttons = {'Ok': this.save};
  70. if (!this.model.isNew()) {
  71. _.extend(buttons, {'Delete': this.destroy});
  72. }
  73. _.extend(buttons, {'Cancel': this.close});
  74. this.el.dialog({
  75. modal: true,
  76. title: (this.model.isNew() ? 'New' : 'Edit') + ' Event',
  77. buttons: buttons,
  78. open: this.open
  79. });
  80. return this;
  81. },
  82. open: function() {
  83. this.$('#title').val(this.model.get('title'));
  84. this.$('#color').val(this.model.get('color'));
  85. },
  86. save: function() {
  87. this.model.set({'title': this.$('#title').val(), 'color': this.$('#color').val()});
  88. if (this.model.isNew()) {
  89. this.collection.create(this.model, {success: this.close});
  90. } else {
  91. this.model.save({}, {success: this.close});
  92. }
  93. },
  94. close: function() {
  95. this.el.dialog('close');
  96. },
  97. destroy: function() {
  98. this.model.destroy({success: this.close});
  99. }
  100. });
  101. var events = new Events();
  102. new EventsView({el: $("#calendar"), collection: events}).render();
  103. events.fetch();
  104. });