Created
March 5, 2009 17:36
-
-
Save oddlyzen/74448 to your computer and use it in GitHub Desktop.
This file contains 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
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