diff options
| author | mo khan <mo@mokhan.ca> | 2014-06-18 20:44:47 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-06-18 20:44:47 -0600 |
| commit | d162cdeabd184292320baf378f0e2c8a7bb61bb7 (patch) | |
| tree | 286b5aac0ab92afe83b5995fa5fde7be84676425 /app/assets/javascripts | |
| parent | 4937ef0fbb2c1815779380159bb8c1280439672a (diff) | |
upload new photo using collection create.
Diffstat (limited to 'app/assets/javascripts')
3 files changed, 22 insertions, 31 deletions
diff --git a/app/assets/javascripts/backbone/routers/photos_router.js.coffee b/app/assets/javascripts/backbone/routers/photos_router.js.coffee index fd4000a3..247378f7 100644 --- a/app/assets/javascripts/backbone/routers/photos_router.js.coffee +++ b/app/assets/javascripts/backbone/routers/photos_router.js.coffee @@ -1,27 +1,28 @@ class Cake.Routers.PhotosRouter extends Backbone.Router routes: "cakes/:cake_id/photos/new" : "newPhoto" - "photos/index" : "index" - "photos/:id/edit" : "edit" - "photos/:id" : "show" - "photos/.*" : "index" + "cakes/:cake_id/photos/index" : "index" + "cakes/:cake_id/photos/:id/edit" : "edit" + "cakes/:cake_id/photos/:id" : "show" + "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) $("#backbone-content").html(@view.render().el) - index: -> + index: (cake_id) -> + @photos = new Cake.Collections.PhotosCollection(cake_id: cake_id) @view = new Cake.Views.Photos.IndexView(photos: @photos) $("#backbone-content").html(@view.render().el) - show: (id) -> + show: (cake_id, id) -> photo = @photos.get(id) @view = new Cake.Views.Photos.ShowView(model: photo) $("#backbone-content").html(@view.render().el) - edit: (id) -> + edit: (cake_id, id) -> photo = @photos.get(id) @view = new Cake.Views.Photos.EditView(model: photo) diff --git a/app/assets/javascripts/backbone/templates/cakes/show.jst.ejs b/app/assets/javascripts/backbone/templates/cakes/show.jst.ejs index c55d0b09..28ec1111 100644 --- a/app/assets/javascripts/backbone/templates/cakes/show.jst.ejs +++ b/app/assets/javascripts/backbone/templates/cakes/show.jst.ejs @@ -1,15 +1,12 @@ -<a href="#/">Back</a> -<a href="#/cakes/<%= id %>/photos" class="btn">Add Photo</a> <div class="row"> <div class="span6"> <% if(_.any(photos)) { %> - <img class="thumbnail" src="<%= photos[0].large_url %>" alt="<%= name %>" /> + <img class="thumbnail" src="<%= photos[Math.floor(Math.random()*photos.length)].large_url %>" alt="<%= name %>" /> <% } %> </div> <div class="span6"> <h1><a href="#/<%= id %>"><%= name %></a></h1> - <p>By <a href="<%= Routes.profile_path(user.id) %>"><%= user.name %></a></p> <span> <i class="icon-tags"></i> </span> <% _.each(tags, function(tag){ %> <a href="<%= Routes.creation_tag_path(tag.name) %>"> @@ -18,6 +15,7 @@ <% }); %> <hr /> <p><%= story %></p> + <a href="#/cakes/<%= id %>/photos/new" class="btn">Add Photo</a> </div> </div> diff --git a/app/assets/javascripts/backbone/views/photos/new_view.js.coffee b/app/assets/javascripts/backbone/views/photos/new_view.js.coffee index a64c6c37..de55d064 100644 --- a/app/assets/javascripts/backbone/views/photos/new_view.js.coffee +++ b/app/assets/javascripts/backbone/views/photos/new_view.js.coffee @@ -21,27 +21,19 @@ class Cake.Views.Photos.NewView extends Backbone.View @model.unset("errors") fileObject = @$(':input[type="file"]')[0].files[0] - photo = new Cake.Models.Photo(cake_id: 100) - photo.url = => @collection.url - photo.set('image', fileObject) - photo.on('progress', console.log) - photo.save() - - #@model.set('image', fileObject) - #@model.save() - #@model.on('progress', console.log) - #@collection.create(@model.toJSON(), - #success: (photo) => - #@model = photo - #window.location.hash = "/#{@model.id}" - - #error: (photo, jqXHR) => - #@model.set({errors: $.parseJSON(jqXHR.responseText)}) - #) + @model.set('image', fileObject) + @model.on('progress', console.log) + @collection.create(@model.toJSON(), + success: (photo) => + @model = photo + debugger + window.location.hash = "/#{@model.cake_id}" + + error: (photo, jqXHR) => + @model.set({errors: $.parseJSON(jqXHR.responseText)}) + ) render: -> - $(@el).html(@template(@model.toJSON() )) - + $(@el).html(@template(@model.toJSON())) this.$("form").backboneLink(@model) - return this |
