From 37487c1600e8aabd922dbb040ff19c9b1179b486 Mon Sep 17 00:00:00 2001 From: jimi Date: Wed, 16 Nov 2011 19:38:18 -0600 Subject: [PATCH] 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 --- main.rb | 12 +++++++++--- views/application.coffee | 12 ++++++++++++ views/index.haml | 1 + views/layout.haml | 4 +++- views/styles.scss | 1 + views/test.coffee | 3 +++ 6 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 views/application.coffee create mode 100644 views/test.coffee diff --git a/main.rb b/main.rb index 7dfc58e..91845bc 100644 --- a/main.rb +++ b/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 diff --git a/views/application.coffee b/views/application.coffee new file mode 100644 index 0000000..127482e --- /dev/null +++ b/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 + diff --git a/views/index.haml b/views/index.haml index a77f80f..5f1222a 100644 --- a/views/index.haml +++ b/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? diff --git a/views/layout.haml b/views/layout.haml index 083300d..445212e 100644 --- a/views/layout.haml +++ b/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 diff --git a/views/styles.scss b/views/styles.scss index fb4f570..d76e38e 100644 --- a/views/styles.scss +++ b/views/styles.scss @@ -72,4 +72,5 @@ h1 { padding:0 10px; } } +#waiting{display:none;position:absolute;left:200px;top:100px;} diff --git a/views/test.coffee b/views/test.coffee new file mode 100644 index 0000000..ed0d1fc --- /dev/null +++ b/views/test.coffee @@ -0,0 +1,3 @@ +$(document).ready -> + $('footer').click -> + alert('Test')