/gallery.js

https://github.com/FeipingHunag/backbonejs-gallery · JavaScript · 228 lines · 120 code · 49 blank · 59 comment · 7 complexity · 00da79ed0d23c50f6063cbe0e4b7c0fa MD5 · raw file

  1. var cache = new CacheProvider;
  2. /**
  3. *Here we create the model 'Photo' ; used to define individual image items. 'subalbum' returns a
  4. *reference to the current subalbum being viewed via the gallery for use when accessing a Photo item
  5. *through a hash URL We also define a new CacheProvider for use in our Controller later.
  6. * @type Backbone.Model
  7. */
  8. var Photo = Backbone.Model.extend({
  9. subalbum: function() { return 'c' + gallery._currentsub; }
  10. });
  11. /**
  12. * PhotoCollection: A collection of Photo items used in index, subalbum and photo views
  13. * @type Backbone.Collection
  14. */
  15. var PhotoCollection = Backbone.Collection.extend({
  16. model: Photo,
  17. comparator: function(item) {
  18. return item.get('pid');
  19. }
  20. });
  21. function removeFallbacks(){
  22. var query = $('.jstest,.gallery');
  23. if(query.length){
  24. query.remove();
  25. }
  26. }
  27. /**
  28. * IndexView: The default view seen when opening up the application for the first time. This
  29. * contains the first level of images in the JSON store (the level-one albums). Prior to rendering
  30. * our jQuery templates here we remove any messages or elements displayed in the version where
  31. * JavaScript is disabled.
  32. * @type Backbone.View
  33. */
  34. var IndexView = Backbone.View.extend({
  35. el: $('#main'),
  36. indexTemplate: $("#indexTmpl").template(),
  37. render: function() {
  38. removeFallbacks();
  39. var sg = this;
  40. this.el.fadeOut('fast', function() {
  41. sg.el.empty();
  42. $.tmpl(sg.indexTemplate, sg.model.toArray()).appendTo(sg.el);
  43. sg.el.fadeIn('fast');
  44. });
  45. return this;
  46. }
  47. });
  48. /**
  49. * SubalbumView: The view reached when clicking on a level-one album or browsing to a subalbum bookmark.
  50. * This contains the images found in the 'subalbum' section of an album entry. Clicking on any of the
  51. * images shown in a subalbum takes you to the PhotoView of that specific image
  52. * @type Backbone.View
  53. */
  54. var SubalbumView = Backbone.View.extend({
  55. el: $('#main'),
  56. indexTemplate: $("#subindexTmpl").template(),
  57. initialize: function(options){
  58. },
  59. render: function() {
  60. var sg = this;
  61. removeFallbacks();
  62. this.el.fadeOut('fast', function() {
  63. sg.el.empty();
  64. $.tmpl(sg.indexTemplate, sg.model.toArray()).appendTo(sg.el);
  65. sg.el.fadeIn('fast');
  66. });
  67. return this;
  68. }
  69. });
  70. /**
  71. * PhotoView: The single-photo view for a single image on the third-level of the application.
  72. * This is reached either by clicking on an image at the second/subalbum level or
  73. * browsing to a bookmarked photo in a subalbum.
  74. * @type Backbone.View
  75. */
  76. var PhotoView = Backbone.View.extend({
  77. el: $('#main'),
  78. itemTemplate: $("#itemTmpl").template(),
  79. initialize: function(options) {
  80. this.album = options.album;
  81. },
  82. render: function() {
  83. var sg = this;
  84. removeFallbacks();
  85. this.el.fadeOut('fast', function() {
  86. sg.el.empty();
  87. $.tmpl(sg.itemTemplate, sg.model).appendTo(sg.el);
  88. sg.el.fadeIn('fast');
  89. });
  90. return this;
  91. }
  92. });
  93. /**
  94. * Gallery: The controller that defines our main application 'gallery'. Here we handle how
  95. * routes should be interpreted, the basic initialization of the application with data through
  96. * an $.ajax call to fetch our JSON store and the creation of collections and views based on the
  97. * models defined previously.
  98. * @type Backbone.Controller
  99. */
  100. var Gallery = Backbone.Controller.extend({
  101. _index: null,
  102. _photos: null,
  103. _album :null,
  104. _subalbums:null,
  105. _subphotos:null,
  106. _data:null,
  107. _photosview:null,
  108. _currentsub:null,
  109. routes: {
  110. "": "index",
  111. "subalbum/:id": "hashsub",
  112. "subalbum/:id/" : "directphoto",
  113. "subalbum/:id/:num" : "hashphoto"
  114. },
  115. initialize: function(options) {
  116. var ws = this;
  117. if (this._index === null){
  118. $.ajax({
  119. url: 'data/album1.json',
  120. dataType: 'json',
  121. data: {},
  122. success: function(data) {
  123. ws._data = data;
  124. ws._photos = new PhotoCollection(data);
  125. ws._index = new IndexView({model: ws._photos});
  126. Backbone.history.loadUrl();
  127. }
  128. });
  129. return this;
  130. }
  131. return this;
  132. },
  133. /**
  134. * Handle rendering the initial view for the application
  135. * @type function
  136. */
  137. index: function() {
  138. this._index.render();
  139. },
  140. /**
  141. * Gallery -> hashsub: Handle URL routing for subalbums. As subalbums aren't traversed
  142. * in the default initialization of the app, here we create a new PhotoCollection for a
  143. * particular subalbum based on indices passed through the UI. We then create a new SubalbumView
  144. * instance, render the subalbums and set the current subphotos array to contain our subalbum Photo
  145. * items. All of this is cached using the CacheProvider we defined earlier
  146. * @type function
  147. * @param {String} id An ID specific to a particular subalbum based on CIDs
  148. */
  149. hashsub:function(id){
  150. var properindex = id.replace('c','');
  151. this._currentsub = properindex;
  152. this._subphotos = cache.get('pc' + properindex) || cache.set('pc' + properindex, new PhotoCollection(this._data[properindex].subalbum));
  153. this._subalbums = cache.get('sv' + properindex) || cache.set('sv' + properindex, new SubalbumView({model: this._subphotos}));
  154. this._subalbums.render();
  155. },
  156. directphoto: function(id){
  157. },
  158. /**
  159. * Gallery -> hashphoto: Handle routing for access to specific images within subalbums. This method
  160. * checks to see if an existing subphotos object exists (ie. if we've already visited the
  161. * subalbum before). If it doesn't, we generate a new PhotoCollection and finally create
  162. * a new PhotoView to display the image that was being queried for. As per hashsub, variable/data
  163. * caching is employed here too
  164. * @type function
  165. * @param {String} num An ID specific to a particular image being accessed
  166. * @param {Integer} id An ID specific to a particular subalbum being accessed
  167. */
  168. hashphoto: function(num, id){
  169. this._currentsub = num;
  170. num = num.replace('c','');
  171. if(this._subphotos == undefined){
  172. this._subphotos = cache.get('pc' + num) || cache.set('pc' + num, new PhotoCollection(this._data[num].subalbum));
  173. }
  174. this._subphotos.at(id)._view = new PhotoView({model: this._subphotos.at(id)});
  175. this._subphotos.at(id)._view.render();
  176. }
  177. });
  178. gallery = new Gallery();
  179. Backbone.history.start();