-
-
Save sebastjan-hribar/2360adf7245ba4db3ea5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Camping.goes :Ajax | |
module Ajax::Controllers | |
class Index < R '/' | |
def get | |
render :index | |
end | |
end | |
class Add < R '/add' | |
def get | |
@todo = @input.todo | |
render :_add | |
end | |
end | |
class Resource < R '/resources/(.+)' | |
def get resource | |
types = { | |
'.css' => 'text/css', '.js' => 'text/javascript', | |
'.jpg' => 'image/jpeg', '.jpeg' => 'image/jpeg', | |
'.gif' => 'image/gif', '.png' => 'image/png' | |
} | |
full_path = File.expand_path(File.dirname(FILE)) + | |
"/resources/#{resource}" | |
if resource.include? ".." # prevent directory traversal attacks | |
@status = "403" | |
"403 - Forbidden" | |
elsif not File.exists?(full_path) | |
@status = "404" | |
"404 - File not found" | |
else | |
@headers['Content-Type'] = types[resource[/.\w+$/]]||"text/plain" | |
@headers['X-Sendfile'] = full_path | |
end | |
end | |
end | |
end | |
module Ajax::Views | |
#Camping::Mab.set(:indent, 2) | |
def layout | |
html do | |
head do | |
title 'Taking AJAX Camping' | |
script :src => R(Resource, 'jquery.js'), :type => 'text/javascript' | |
end | |
body { self << yield } | |
end | |
end | |
def index | |
script :type => 'text/javascript' do " | |
$(document).ready(function() { | |
$('#todo').focus(); | |
$('#add').submit(function() { | |
var value = $('#todo').val(); | |
$('#todo').val(''); // reset input box | |
if (value) { // don't add empty todos | |
$.ajax({ | |
type: 'GET', | |
url: '/add', | |
data: 'todo=' + value, | |
success: function(msg){ | |
// Add the todo to the top of the list | |
$('#todos').prepend('<li>' + msg + '</li>'); | |
} | |
}); | |
} | |
// focus the input field when the document loads | |
$('#todo').focus(); | |
return false; | |
}); | |
});" | |
end | |
h1 'Taking AJAX Camping' | |
form.add! do | |
legend "Another todo list that doesn't work" | |
input.todo! :type => 'text', :name => 'todo' | |
end | |
ul.todos! "" | |
end | |
def _add | |
text "#{@todo}: #{Time.now.to_s}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment