Created
December 16, 2008 17:12
-
-
Save ahoward/36666 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
# generates a div that will load itself with content from an ajax request. | |
# the request is un-altered so, if you don't want it wrapped in the | |
# header/footer you may need to pass in 'partial=true' with the query string | |
# | |
# eg: ajax_content_for :url => '/content/some-path' | |
# | |
# eg: ajax_content_for '/content/some-path' | |
# | |
def ajax_content_for *args | |
options = args.options | |
options.to_options! | |
url = | |
if options[:url] | |
options[:url].to_s | |
else | |
args.join | |
end | |
slug = url.slug | |
id = "content-for-#{ slug }" | |
selector = "##{ id }" | |
<<-html | |
<div id=#{ id.inspect }> </div> | |
<script type="text/javascript"> | |
(function(){ | |
jq.ajax({ | |
'url' : #{ url.to_json }, | |
'async' : false, | |
'success' : function(data, status){ jq(#{ selector.to_json }).html(data) }, | |
'error' : function(data, status){ jq(#{ selector.to_json }).html('Sorry, ' + #{ url.to_json } + ' not found') }, | |
}); | |
})(); | |
</script> | |
html | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment