Sha256: 3057d025f2e5595c33e46cae166b0fac73d26b576be52d6b47af1d531d07b58c

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

module JellyController
  protected
  include Jelly::Common

  def jelly_callback(callback_base_name = @action_name, options = {}, &block)
    raw_jelly_callback(options) do
      arguments = block ? instance_eval(&block) : []
      arguments = [arguments] unless arguments.is_a?(Array)
      jelly_callback_hash("on_#{callback_base_name}", *arguments).merge(options)
    end
  end

  def raw_jelly_callback(options={}, &block)
    options.symbolize_keys!
    options[:format] ||= if params[:callback]
      :jsonp
    elsif request.xhr?
      :json
    else
      :iframe
    end
    render :inline => jelly_callback_erb(options, &block)
  end

  def jelly_callback_erb(options={}, &block)
    options[:format] ||= :json
    @jelly_block = block
    case options[:format].to_sym
      when :iframe
        "<textarea>#{jelly_callback_erb_template}</textarea>"
      when :jsonp
        "#{params[:callback]}(#{jelly_callback_erb_template});"
      else
        jelly_callback_erb_template
    end
  end

  def jelly_callback_erb_template
    "<%= instance_eval(&@jelly_block).to_json %>"
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
jelly-0.8.10 lib/jelly/jelly_controller.rb
honkster-jelly-0.8.10 lib/jelly/jelly_controller.rb
honkster-jelly-0.8.9 lib/jelly/jelly_controller.rb
honkster-jelly-0.8.8 lib/jelly/jelly_controller.rb
honkster-jelly-0.8.7 lib/jelly/jelly_controller.rb