Commit ea7d5417

mo khan <mo@mokhan.ca>
2014-06-29 14:12:37
re-render thumbnail when photo model synced and create cache for photo collections.
1 parent 2495bb0
Changed files (4)
app
assets
app/assets/javascripts/backbone/models/photo.js.coffee
@@ -4,6 +4,7 @@ class CakeSide.Models.Photo extends Backbone.Model
 
   defaults:
     id: null
+    cake_id: null
     content_type: null
     original_filename: null
     thumb_url: null
@@ -16,4 +17,5 @@ class CakeSide.Collections.PhotosCollection extends Backbone.Collection
   model: CakeSide.Models.Photo
 
   initialize: (options) ->
+    @set('cake_id', options.cake_id)
     @url="/api/v1/cakes/#{options.cake_id}/photos"
app/assets/javascripts/backbone/templates/cakes/thumbnail.jst.ejs
@@ -1,5 +1,5 @@
 <li class="span2">
-<a href="<%= Routes.creation_photo_path(cake_id, id || 0) %>">
+<a href="<%= Routes.creation_photo_path(cake_id || 0, id || 0) %>">
   <img src="<%= thumb_url %>" class="thumbnail" />
 </a>
 </li>
app/assets/javascripts/backbone/views/cakes/thumbnail_view.js.coffee
@@ -2,3 +2,5 @@ CakeSide.Views.Cakes ||= {}
 
 class CakeSide.Views.Cakes.ThumbnailView extends Marionette.ItemView
   template: JST['backbone/templates/cakes/thumbnail']
+  modelEvents:
+    'sync': 'render'
app/assets/javascripts/backbone/cakeside.js.coffee
@@ -31,10 +31,15 @@ window.CakeSide =
       @cakes
     CakeSide.Application.reqres.setHandler 'CategoriesRepository', =>
       @categories
-    CakeSide.Application.reqres.setHandler 'PhotosRepository', (cake_id) ->
-      photos = new CakeSide.Collections.PhotosCollection(cake_id: cake_id)
-      photos.fetch(reset: true)
-      photos
+    @photos_cache = {}
+    CakeSide.Application.reqres.setHandler 'PhotosRepository', (cake_id) =>
+      if @photos_cache[cake_id]
+        @photos_cache[cake_id]
+      else
+        photos = new CakeSide.Collections.PhotosCollection(cake_id: cake_id)
+        @photos_cache[cake_id] = photos
+        photos.fetch(reset: true)
+        photos
 
     @cakes.fetch(reset: true).done ->
       CakeSide.Application.start()