fanforce 0.3.6 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/fanforce/utils.rb +23 -0
- data/lib/fanforce/version.rb +1 -1
- metadata +1 -1
data/lib/fanforce/utils.rb
CHANGED
@@ -120,4 +120,27 @@ module Fanforce::Utils
|
|
120
120
|
MultiJson.load(str, :symbolize_keys => symbolize_keys)
|
121
121
|
end
|
122
122
|
|
123
|
+
# Creates a string representation of a javascript object for $.tmpl
|
124
|
+
def compile_jquery_tmpls(options={}, &block)
|
125
|
+
begin require 'haml' rescue LoadError raise "You must have the haml gem installed for Fanforce.compile_jquery_templates to work." end
|
126
|
+
context = Object.new
|
127
|
+
class << context
|
128
|
+
include Haml::Helpers
|
129
|
+
end
|
130
|
+
context.init_haml_helpers
|
131
|
+
|
132
|
+
format = if options[:format] == 'html' then :html else options[:callback].present? ? :jsonp : :json end
|
133
|
+
|
134
|
+
return context.capture_haml(&block) if format == :html
|
135
|
+
single_line_html = context.capture_haml(&block).split(/\r?\n/).inject('') {|sl, l| sl += l.strip + ' ' }
|
136
|
+
matches = single_line_html.scan(/<script id=[\"'](.*?)[\"'](?:.*?)>(.*?)(?:<\/script>)/mi)
|
137
|
+
|
138
|
+
templates = matches.inject({}) {|t,m| t[m[0]] = m[1]; t }
|
139
|
+
if format == :jsonp
|
140
|
+
"#{options[:callback]}(#{templates.to_json})"
|
141
|
+
else
|
142
|
+
templates.to_json
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
123
146
|
end
|
data/lib/fanforce/version.rb
CHANGED