Permalink
Browse files

More UI

  • Loading branch information...
1 parent 6dc23d8 commit f00875e9f41819455eac1937484adaaeaf34d665 @knewter committed Feb 19, 2012
Showing with 227 additions and 18 deletions.
  1. +12 −1 api/api.rb
  2. +81 −4 coffee/lib/wine_app.coffee
  3. +121 −10 public/javascript/wine_app.js
  4. +13 −3 public/winelist.html
View
@@ -4,15 +4,26 @@ class API < Grape::API
prefix 'api'
version 'v1'
- resource :wines do
+ resources :wines do
desc "Returns some wines."
get do
Wine.all
end
+ desc "Get a specific wine's details"
get ':id' do
Wine.find(id)
end
+
+ desc "Update a wine"
+ put ':id' do
+ STDOUT.puts "---"
+ STDOUT.puts "foo"
+ STDOUT.puts "---"
+ wine = Wine.find(id)
+ wine.update_attributes(params)
+ wine
+ end
end
end
end
View
@@ -1,5 +1,15 @@
jQuery ->
class Wine extends Backbone.Model
+ urlRoot: '/api/v1/wines'
+ defaults:
+ "id": null
+ "name": ""
+ "grapes": ""
+ "country": "USA"
+ "region": "California"
+ "year": ""
+ "description": ""
+ "picture": ""
class WineCollection extends Backbone.Collection
model: Wine
@@ -11,6 +21,8 @@ jQuery ->
initialize: ->
_.bindAll @
@model.bind 'reset', @render
+ @model.bind 'add', (wine) =>
+ $(@el).append new WineListItemView(model: wine).render().el
render: ->
_.each @model.models, (wine) =>
@@ -23,23 +35,84 @@ jQuery ->
initialize: ->
_.bindAll @
+ @model.bind 'change', @render
+ @model.bind 'destroy', @close
template: _.template $('#tpl-wine-list-item').html()
render: ->
$(@el).html(@template(@model.toJSON()))
@
+ close: ->
+ $(@el).unbind()
+ $(@el).remove()
+
class WineView extends Backbone.View
initialize: ->
_.bindAll @
+ @model.bind 'change', @render
template: _.template $('#tpl-wine-details').html()
render: ->
$(@el).html @template(@model.toJSON())
@
+ events:
+ 'change input': 'change'
+ 'click .save': 'saveWine'
+ 'click .delete': 'deleteWine'
+
+ change: (event) ->
+ target = event.target
+ console.log "Changing #{target.id} from #{target.defaultVal}"
+
+ saveWine: ->
+ @model.set
+ name: $('#name').val()
+ grapes: $('#grapes').val()
+ country: $('#country').val()
+ region: $('#region').val()
+ year: $('#year').val()
+ description: $('#description').val()
+ if @model.isNew()
+ app.wineList.create(@model)
+ else
+ @model.save()
+ false
+
+ deleteWine: ->
+ @model.destroy
+ success: ->
+ alert 'Wine deleted successfully'
+ window.history.back()
+ false;
+
+ close: ->
+ $(@el).unbind()
+ $(@el).remove()
+
+ class HeaderView extends Backbone.View
+ template: _.template $('#tpl-header').html()
+
+ initialize: ->
+ _.bindAll @
+ @render()
+
+ render: ->
+ $(@el).html @template()
+ @
+
+ events:
+ "click .new": "newWine"
+
+ newWine: (event) ->
+ app.wineView.close() if app.wineView
+ app.wineView = new WineView model: new Wine()
+ $('#content').html app.wineView.render().el
+ false;
+
class AppRouter extends Backbone.Router
initialize: ->
_.bindAll @
@@ -49,16 +122,20 @@ jQuery ->
"wines/:id": "wineDetails"
list: ->
- @wineList = new WineCollection
- @wineListView = new WineListView(model: @wineList)
- @wineList.fetch()
+ @setup()
$('#sidebar').html @wineListView.render().el
wineDetails: (id) ->
- window.wineList = @wineList
+ @setup()
@wine = @wineList.get id
@wineView = new WineView model: @wine
$('#content').html @wineView.render().el
+ setup: ->
+ @wineList ||= new WineCollection
+ @wineList.fetch()
+ @wineListView ||= new WineListView(model: @wineList)
+
+
app = new AppRouter
Backbone.history.start()

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit f00875e

Please sign in to comment.