December 7, 2007 12:05pm
Camping is great, yeah. JQuery too. Maybe you want to use them together? Here's how.
First, you need to set up a route for your JQuery. I've talked about sending static files with Camping before, so this is just a modification of that. [You can serve static files more sensibly than setting up a route for each type. Maybe one day I'll write about that.]
In your controller:module MyApp::Controllers
class Index < R '/'
def get
render :index
end
end
class JQuery < R '/resources/jquery.js'
def get
current_dir = File.expand_path(File.dirname(__FILE__))
@headers['Content-Type'] = "text/javascript"
@headers['X-Sendfile'] = "#{current_dir}/resources/jquery.js"
end
end
end
Now you have to get the JQuery library in your view.module MyApp::Views
def layout
html do
head do
title 'Using JQuery in Camping'
end
body { self << yield }
end
end
def index
script :src => R(JQuery), :type => 'text/javascript'
script do '
$(document).ready(function() {
alert("Hello world!");
});
'
end
end
end
The call to the R function creates the URL for the JQuery library. Now you can embed any JQuery yumminess that you want, right there in your view.
December 21st, 2007 at 12:38pm
[...] that you’ve got JQuery in your Camping application, one of the things you might want to do is take advantage of JQuery’s AJAX [...]