Skip to content

Instantly share code, notes, and snippets.

@oddlyzen
Created March 5, 2009 17:36
Show Gist options
  • Save oddlyzen/74448 to your computer and use it in GitHub Desktop.
Save oddlyzen/74448 to your computer and use it in GitHub Desktop.
def serialize_to_json(options={})
namespace = options[:namespace].nil? ? 'ZEP' : options[:namespace]
if options[:data]
list = options[:data]
js = ""
js << "<script type=\"text/javascript\">"
js << "\n\tif (typeof #{namespace} !== 'object') { var #{namespace} = {}; }\n"
js << "\n\tif (typeof #{namespace}.models !== 'object') { #{namespace}.models = {}; }\n"
list.each do |obj|
if obj.respond_to? :to_json
js << "\t#{namespace}.models.#{obj.class.to_s.downcase} = " + obj.to_json + ";\n" unless obj.nil?
end
end
js << "</script>"
else
raise ArgumentException, "Key-value pair (options[:data]) expected."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment