Browse Source

Manage robots with Ajax requests

- include RightJS and app specific js files
  - use coffeescript for app specific code
  - make routes check for xhr data
  - add spinner for xhr requests
master
jimi 13 years ago
parent
commit
37487c1600
  1. 12
      main.rb
  2. 12
      views/application.coffee
  3. 1
      views/index.haml
  4. 4
      views/layout.haml
  5. 1
      views/styles.scss
  6. 3
      views/test.coffee

12
main.rb

@ -1,4 +1,4 @@
%w[sinatra dm-core dm-migrations haml sass].each{ |lib| require lib }
%w[sinatra dm-core dm-migrations haml sass coffee_script].each{ |lib| require lib }
DataMapper.setup(:default, File.join("sqlite3://",settings.root,"development.db"))
class Robot
@ -13,6 +13,8 @@ DataMapper.finalize
get ('/styles.css'){ scss :styles }
get('/application.js'){ coffee :application }
get '/' do
@robots = Robot.all
haml :index
@ -20,10 +22,14 @@ end
post '/build/robot' do
robot = Robot.create
redirect to('/')
if request.xhr?
haml :robot, { layout: false, locals: { robot: robot } }
else
redirect to('/')
end
end
delete '/delete/robot/:id' do
Robot.get(params[:id]).destroy
redirect to('/')
redirect to('/') unless request.xhr?
end

12
views/application.coffee

@ -0,0 +1,12 @@
Xhr.Options.spinner = 'waiting'
"form.destroy".onSubmit (event) ->
@parent().fade()
event.stop()
@send()
"form.build".onSubmit (event) ->
event.stop()
@send onSuccess: (xhr) ->
$("robots").insert xhr.responseText

1
views/index.haml

@ -1,4 +1,5 @@
%h1 Robot Factory
%img#waiting{ src:"https://s3.amazonaws.com/daz4126/waiting.gif" }
%form.build{ action:"/build/robot", method:"POST" }
%input.button{ type:"submit", value:"Build A Robot!" }
-if @robots.any?

4
views/layout.haml

@ -1,4 +1,4 @@
!!! 5
!!!5
%html
%head
%meta{ charset: "utf-8" }
@ -9,6 +9,8 @@
%link{ rel: "stylesheet", href: "/styles.css" }
/[if lt IE 9]
%script{ src: "http://html5shiv.googlecode.com/svn/trunk/html5.js" }
%script{ src:"http://cdn.rightjs.org/right.js" }
%script{ src:"/application.js" }
%body
= yield
#footer

1
views/styles.scss

@ -72,4 +72,5 @@ h1 {
padding:0 10px;
}
}
#waiting{display:none;position:absolute;left:200px;top:100px;}

3
views/test.coffee

@ -0,0 +1,3 @@
$(document).ready ->
$('footer').click ->
alert('Test')