Skip to content

Instantly share code, notes, and snippets.

@mnelson
Created November 24, 2012 06:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mnelson/4138685 to your computer and use it in GitHub Desktop.
Save mnelson/4138685 to your computer and use it in GitHub Desktop.
Use jbuilder to render jsonp responses.
raw_content = JSON.parse(yield)
json.content raw_content
json.jsonp! params[:callback] if jsonp?
Mime::EXTENSION_LOOKUP['jsonp'] = Mime::Type.lookup_by_extension('json')
require 'jbuilder'
module Jsonp::Jbuilder
extend ActiveSupport::Concern
included do
alias_method_chain :target!, :jsonp
end
def jsonp!(callback_name)
@jsonp_callback = callback_name
end
def target_with_jsonp!
json = target_without_jsonp!
if @jsonp_callback
"#{@jsonp_callback}(#{json})"
else
json
end
end
end
::Jbuilder.send :include, Jsonp::Jbuilder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment