Commit 1587bb47

mo khan <mo@mokhan.ca>
2014-06-28 14:47:54
load up photos in a composite view.
1 parent e081f9b
app/assets/javascripts/backbone/models/cake.js.coffee
@@ -1,4 +1,4 @@
-class Cake.Models.Cake extends Backbone.Model
+class CakeSide.Models.Cake extends Backbone.Model
   paramRoot: 'cake'
 
   defaults:
@@ -10,6 +10,9 @@ class Cake.Models.Cake extends Backbone.Model
     return "Name can't be blank" unless attributes.name && attributes.name.trim()
     return "Category can't be blank" unless attributes.category_id
 
-class Cake.Collections.CakesCollection extends Backbone.Collection
-  model: Cake.Models.Cake
+  photos: ->
+    photos = CakeSide.Application.request('PhotosRepository', @id)
+
+class CakeSide.Collections.CakesCollection extends Backbone.Collection
+  model: CakeSide.Models.Cake
   url: '/api/v1/cakes'
app/assets/javascripts/backbone/models/photo.js.coffee
@@ -1,14 +1,19 @@
-class Cake.Models.Photo extends Backbone.Model
+class CakeSide.Models.Photo extends Backbone.Model
   paramRoot: 'photo'
   fileAttribute: 'image'
 
   defaults:
+    id: 0
+    content_type: null
+    original_filename: null
     thumb_url: null
     large_url: null
     original_url: null
+    created_at: null
+    updated_at: null
 
-class Cake.Collections.PhotosCollection extends Backbone.Collection
-  model: Cake.Models.Photo
+class CakeSide.Collections.PhotosCollection extends Backbone.Collection
+  model: CakeSide.Models.Photo
 
   initialize: (options) ->
     @url="/api/v1/cakes/#{options.cake_id}/photos"
app/assets/javascripts/backbone/routers/cakes_router.js.coffee
@@ -1,4 +1,4 @@
-class Cake.Routers.CakesRouter extends Backbone.Router
+class CakeSide.Routers.CakesRouter extends Backbone.Router
   routes:
     "new"      : "newCake"
     "index"    : "index"
@@ -7,20 +7,20 @@ class Cake.Routers.CakesRouter extends Backbone.Router
     ".*"       : "index"
 
   index: ->
-    @view = new Cake.Views.Cakes.IndexView(collection: Cake.Application.request('CakeRepository'))
+    @view = new CakeSide.Views.Cakes.IndexView(collection: CakeSide.Application.request('CakesRepository'))
     $("#backbone-content").html(@view.render().el)
 
   show: (id) ->
-    cake = Cake.Application.request('CakeRepository').get(id)
-    @view = new Cake.Views.Cakes.ShowView(model: cake)
+    cake = CakeSide.Application.request('CakesRepository').get(id)
+    @view = new CakeSide.Views.Cakes.ShowView(model: cake)
     $("#backbone-content").html(@view.render().el)
 
   newCake: ->
-    @view = new Cake.Views.Cakes.NewView(collection: Cake.Application.request('CakeRepository'))
+    @view = new CakeSide.Views.Cakes.NewView(collection: CakeSide.Application.request('CakesRepository'))
     $("#backbone-content").html(@view.render().el)
 
   edit: (id) ->
-    cake = Cake.Application.request('CakeRepository').get(id)
+    cake = CakeSide.Application.request('CakesRepository').get(id)
 
-    @view = new Cake.Views.Cakes.EditView(model: cake)
+    @view = new CakeSide.Views.Cakes.EditView(model: cake)
     $("#backbone-content").html(@view.render().el)
app/assets/javascripts/backbone/routers/photos_router.js.coffee
@@ -1,4 +1,4 @@
-class Cake.Routers.PhotosRouter extends Backbone.Router
+class CakeSide.Routers.PhotosRouter extends Backbone.Router
   routes:
     "cakes/:cake_id/photos/new"      : "newPhoto"
     "cakes/:cake_id/photos/index"    : "index"
@@ -7,23 +7,23 @@ class Cake.Routers.PhotosRouter extends Backbone.Router
     "cakes/:cake_id/photos/.*"        : "index"
 
   newPhoto: (cake_id) ->
-    @photos = new Cake.Collections.PhotosCollection(cake_id: cake_id)
-    @view = new Cake.Views.Photos.NewView(collection: @photos)
+    @photos = new CakeSide.Collections.PhotosCollection(cake_id: cake_id)
+    @view = new CakeSide.Views.Photos.NewView(collection: @photos)
     $("#backbone-content").html(@view.render().el)
 
   index: (cake_id) ->
-    @photos = new Cake.Collections.PhotosCollection(cake_id: cake_id)
-    @view = new Cake.Views.Photos.IndexView(photos: @photos)
+    @photos = new CakeSide.Collections.PhotosCollection(cake_id: cake_id)
+    @view = new CakeSide.Views.Photos.IndexView(photos: @photos)
     $("#backbone-content").html(@view.render().el)
 
   show: (cake_id, id) ->
     photo = @photos.get(id)
 
-    @view = new Cake.Views.Photos.ShowView(model: photo)
+    @view = new CakeSide.Views.Photos.ShowView(model: photo)
     $("#backbone-content").html(@view.render().el)
 
   edit: (cake_id, id) ->
     photo = @photos.get(id)
 
-    @view = new Cake.Views.Photos.EditView(model: photo)
+    @view = new CakeSide.Views.Photos.EditView(model: photo)
     $("#backbone-content").html(@view.render().el)
app/assets/javascripts/backbone/templates/cakes/show.jst.ejs
@@ -24,14 +24,6 @@
     <a href="<%= Routes.creation_photos_path(id) %>">View photos &raquo;</a>
   </div>
   <div class="span12">
-    <ul class="thumbnails">
-    <% _.each(photos, function(photo) { %>
-      <li class="span2">
-      <a href="<%= Routes.creation_photo_path(id, photo.id) %>">
-        <img src="<%= photo.thumb_url %>" class="thumbnail" />
-      </a>
-      </li>
-    <% }); %>
-    </ul>
+    <ul class="thumbnails"></ul>
   </div>
 </div>
app/assets/javascripts/backbone/templates/cakes/thumbnail.jst.ejs
@@ -0,0 +1,5 @@
+<li class="span2">
+<a href="<%= Routes.creation_photo_path(cake_id, id) %>">
+  <img src="<%= thumb_url %>" class="thumbnail" />
+</a>
+</li>
app/assets/javascripts/backbone/views/cakes/cake_view.js.coffee
@@ -1,4 +1,4 @@
-Cake.Views.Cakes ||= {}
+CakeSide.Views.Cakes ||= {}
 
-class Cake.Views.Cakes.CakeView extends Marionette.ItemView
+class CakeSide.Views.Cakes.CakeView extends Marionette.ItemView
   template: JST["backbone/templates/cakes/cake"]
app/assets/javascripts/backbone/views/cakes/edit_view.js.coffee
@@ -1,6 +1,6 @@
-Cake.Views.Cakes ||= {}
+CakeSide.Views.Cakes ||= {}
 
-class Cake.Views.Cakes.EditView extends Backbone.View
+class CakeSide.Views.Cakes.EditView extends Backbone.View
   template : JST["backbone/templates/cakes/edit"]
 
   events :
app/assets/javascripts/backbone/views/cakes/index_view.js.coffee
@@ -1,4 +1,4 @@
-Cake.Views.Cakes ||= {}
+CakeSide.Views.Cakes ||= {}
 
-class Cake.Views.Cakes.IndexView extends Marionette.CollectionView
-  childView: Cake.Views.Cakes.CakeView
+class CakeSide.Views.Cakes.IndexView extends Marionette.CollectionView
+  childView: CakeSide.Views.Cakes.CakeView
app/assets/javascripts/backbone/views/cakes/new_view.js.coffee
@@ -1,6 +1,6 @@
-Cake.Views.Cakes ||= {}
+CakeSide.Views.Cakes ||= {}
 
-class Cake.Views.Cakes.NewView extends Marionette.ItemView
+class CakeSide.Views.Cakes.NewView extends Marionette.ItemView
   template: JST["backbone/templates/cakes/new"]
   ui:
     name: "#cake_name"
@@ -39,7 +39,7 @@ class Cake.Views.Cakes.NewView extends Marionette.ItemView
     window.location.hash = "/cakes/#{cake.id}/photos/new"
 
   couldNotSave: (cake, xhr) ->
-    error = new Cake.Views.ErrorView
+    error = new CakeSide.Views.ErrorView
       el: @$('form#new-cake'),
       attributesWithErrors: $.parseJSON(xhr.responseText)
     error.render()
app/assets/javascripts/backbone/views/cakes/show_view.js.coffee
@@ -1,20 +1,26 @@
-Cake.Views.Cakes ||= {}
+CakeSide.Views.Cakes ||= {}
 
-class Cake.Views.Cakes.ShowView extends Backbone.View
+class CakeSide.Views.Cakes.ThumbnailView extends Marionette.ItemView
+  template: JST['backbone/templates/cakes/thumbnail']
+
+class CakeSide.Views.Cakes.ShowView extends Marionette.CompositeView
   template: JST["backbone/templates/cakes/show"]
+  childView: CakeSide.Views.Cakes.ThumbnailView
+  childViewContainer: '.thumbnails'
 
   events:
     "click #add-photo": "launchAddPhoto"
 
   constructor: (options) ->
     super(options)
-    @model.on('sync', @render)
+    #@model.on('sync', @render)
+    @collection = @model.photos()
 
-  render: ->
-    $(@el).html(@template(@model.toJSON()))
-    return this
+  #render: ->
+    #$(@el).html(@template(@model.toJSON()))
+    #return this
 
   launchAddPhoto: ->
-    view = new Cake.Views.Photos.NewModalView(cake: @model)
+    view = new CakeSide.Views.Photos.NewModalView(cake: @model)
     $("#modal").html(view.render().el)
     $("#modal").modal()
app/assets/javascripts/backbone/views/photos/edit_view.js.coffee
@@ -1,6 +1,6 @@
-Cake.Views.Photos ||= {}
+CakeSide.Views.Photos ||= {}
 
-class Cake.Views.Photos.EditView extends Backbone.View
+class CakeSide.Views.Photos.EditView extends Backbone.View
   template : JST["backbone/templates/photos/edit"]
 
   events :
app/assets/javascripts/backbone/views/photos/index_view.js.coffee
@@ -1,6 +1,6 @@
-Cake.Views.Photos ||= {}
+CakeSide.Views.Photos ||= {}
 
-class Cake.Views.Photos.IndexView extends Backbone.View
+class CakeSide.Views.Photos.IndexView extends Backbone.View
   template: JST["backbone/templates/photos/index"]
 
   initialize: () ->
@@ -10,7 +10,7 @@ class Cake.Views.Photos.IndexView extends Backbone.View
     @options.photos.each(@addOne)
 
   addOne: (photo) =>
-    view = new Cake.Views.Photos.PhotoView({model : photo})
+    view = new CakeSide.Views.Photos.PhotoView({model : photo})
     @$("tbody").append(view.render().el)
 
   render: =>
app/assets/javascripts/backbone/views/photos/new_modal_view.js.coffee
@@ -1,6 +1,6 @@
-Cake.Views.Photos ||= {}
+CakeSide.Views.Photos ||= {}
 
-class Cake.Views.Photos.NewModalView extends Backbone.View
+class CakeSide.Views.Photos.NewModalView extends Backbone.View
   template: JST["backbone/templates/photos/new-modal"]
 
   events:
@@ -9,7 +9,7 @@ class Cake.Views.Photos.NewModalView extends Backbone.View
 
   constructor: (options) ->
     super(options)
-    @collection = new Cake.Collections.PhotosCollection(cake_id: options.cake.id)
+    @collection = new CakeSide.Collections.PhotosCollection(cake_id: options.cake.id)
     @model = new @collection.model()
     @cake = options.cake
 
app/assets/javascripts/backbone/views/photos/new_view.js.coffee
@@ -1,6 +1,6 @@
-Cake.Views.Photos ||= {}
+CakeSide.Views.Photos ||= {}
 
-class Cake.Views.Photos.NewView extends Backbone.View
+class CakeSide.Views.Photos.NewView extends Backbone.View
   template: JST["backbone/templates/photos/new"]
 
   events:
app/assets/javascripts/backbone/views/photos/photo_view.js.coffee
@@ -1,6 +1,6 @@
-Cake.Views.Photos ||= {}
+CakeSide.Views.Photos ||= {}
 
-class Cake.Views.Photos.PhotoView extends Backbone.View
+class CakeSide.Views.Photos.PhotoView extends Backbone.View
   template: JST["backbone/templates/photos/photo"]
 
   events:
app/assets/javascripts/backbone/views/photos/show_view.js.coffee
@@ -1,6 +1,6 @@
-Cake.Views.Photos ||= {}
+CakeSide.Views.Photos ||= {}
 
-class Cake.Views.Photos.ShowView extends Backbone.View
+class CakeSide.Views.Photos.ShowView extends Backbone.View
   template: JST["backbone/templates/photos/show"]
 
   render: ->
app/assets/javascripts/backbone/views/error_view.js.coffee
@@ -1,6 +1,6 @@
-Cake.Views.Cakes ||= {}
+CakeSide.Views.Cakes ||= {}
 
-class Cake.Views.ErrorView extends Backbone.View
+class CakeSide.Views.ErrorView extends Backbone.View
   initialize: (options) ->
     @attributesWithErrors = options.attributesWithErrors
     _.bindAll(@, "clearOldErrors", "renderErrors", "renderError", "fieldFor")
app/assets/javascripts/backbone/cake.js.coffee
@@ -4,7 +4,7 @@
 #= require_tree ./views
 #= require_tree ./routers
 
-window.Cake =
+window.CakeSide =
   Models: {}
   Collections: {}
   Routers: {}
@@ -14,18 +14,22 @@ window.Cake =
       if data.access_token
         xhr.setRequestHeader "Authorization", "Token token=#{data.access_token}"
 
-    Cake.Application = new Marionette.Application()
-    Cake.Application.addInitializer (options) ->
-      new Cake.Routers.CakesRouter()
-      new Cake.Routers.PhotosRouter()
+    CakeSide.Application = new Marionette.Application()
+    CakeSide.Application.addInitializer (options) ->
+      new CakeSide.Routers.CakesRouter()
+      new CakeSide.Routers.PhotosRouter()
 
-    Cake.Application.on 'start', ->
+    CakeSide.Application.on 'start', ->
       if Backbone.history
         Backbone.history.start()
 
-    @cakes = new Cake.Collections.CakesCollection()
-    Cake.Application.reqres.setHandler 'CakeRepository', =>
+    @cakes = new CakeSide.Collections.CakesCollection()
+    CakeSide.Application.reqres.setHandler 'CakesRepository', =>
       @cakes
+    CakeSide.Application.reqres.setHandler 'PhotosRepository', (cake_id) =>
+      photos = new CakeSide.Collections.PhotosCollection(cake_id: cake_id)
+      photos.fetch(reset: true)
+      photos
 
     @cakes.fetch(reset: true).done ->
-      Cake.Application.start()
+      CakeSide.Application.start()
app/controllers/api/v1/photos_controller.rb
@@ -3,6 +3,10 @@ module Api
     class PhotosController < ApiController
       respond_to :json
 
+      def index
+        respond_with(@photos = current_user.creations.find(params[:cake_id]).photos)
+      end
+
       def create
         cake_id = params[:cake_id]
         UploadPhoto.new.run(cake_id, params)
app/views/api/v1/photos/_photo.json.jbuilder
@@ -2,9 +2,6 @@ json.id photo.id
 json.cake_id photo.creation_id
 json.content_type photo.content_type
 json.original_filename photo.original_filename
-json.latitude photo.latitude
-json.longitude photo.longitude
-json.sha256 photo.sha256
 json.thumb_url photo.url_for(:thumb)
 json.large_url photo.url_for(:large)
 json.original_url photo.url_for(:original)
app/views/api/v1/photos/index.json.jbuilder
@@ -0,0 +1,3 @@
+json.array! @photos do |photo|
+  json.partial! 'photo', photo: photo
+end
app/views/my/cakes/index.html.erb
@@ -1,7 +1,7 @@
 <%= content_for :javascript do -%>
   <%= javascript_tag do %>
     var ALL_TAGS = [ <% ActsAsTaggableOn::Tag.pluck(:name).sort!.each { |item| %> '<%= item %>', <% } %> ];
-    Cake.initialize({ access_token: '<%= current_user.authentication_token %>' });
+    CakeSide.initialize({ access_token: '<%= current_user.authentication_token %>' });
   <% end %>
 <% end -%>
 
config/routes.rb
@@ -47,7 +47,7 @@ Cake::Application.routes.draw do
   namespace :api, :defaults => { :format => 'json' }  do
     namespace :v1 do
       resources :cakes, :only => [:index, :show, :create, :update] do
-        resources :photos, :only => [:create]
+        resources :photos, :only => [:index, :create]
       end
       resources :logins, :only => [:create]
     end