Compiled Json for partially cached json response / precompiled handlebar templates

  • pull part of your json response from cache and do not pay for re-jsoning it
  • return precompiled handlebar templates as jsonp response

Usage

# fast response via precompiled parts
result = user.to_json
result["tickets"] = cache { CompiledJson.new(tickets.to_json) }
render :json => result

# handlebar templates or other crazyness
result = user.to_json
result["tickets"] = CompiledJson.new(
  'function(c){ return "foo" + c.name + "bar" }'
)
render :json => result

code

class CompiledJson < String
  # activesupport
  def encode_json(*args)
    self
  end

  # pure JSON
  def to_json
    self
  end
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s