Created
November 24, 2012 06:42
-
-
Save mnelson/4138685 to your computer and use it in GitHub Desktop.
Use jbuilder to render jsonp responses.
This file contains hidden or 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
raw_content = JSON.parse(yield) | |
json.content raw_content | |
json.jsonp! params[:callback] if jsonp? |
This file contains hidden or 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
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