Module: Hammock::JavascriptBuffer::InstanceMethods
- Defined in:
- lib/hammock/javascript_buffer.rb
Instance Method Summary collapse
-
#append_javascript(snippet) ⇒ Object
Add
snippet
to the request’s domready javascript cache. -
#append_toplevel_javascript(snippet) ⇒ Object
Add
snippet
to the request’s toplevel javascript cache. - #clear_js_caches ⇒ Object
-
#javascript_for_ajax_response ⇒ Object
If the current request is XHR, render all cached javascript as
javascript_for_page
would and clear the request’s javascript cache. -
#javascript_for_page ⇒ Object
Render the snippets cached by
append_javascript
andappend_toplevel_javascript
within a<script type="text/javascript">
tag.
Instance Method Details
#append_javascript(snippet) ⇒ Object
Add snippet
to the request’s domready javascript cache.
The contents of this cache can be rendered into a jQuery $(function() { ... })
block within a <script type="text/javascript">
block by calling javascript_for_page
within the <head> of the layout.
18 19 20 21 22 |
# File 'lib/hammock/javascript_buffer.rb', line 18 def append_javascript snippet # TODO This should be an array of strings. @_domready_javascript ||= '' @_domready_javascript << snippet.strip.end_with(';') << "\n\n" unless snippet.nil? end |
#append_toplevel_javascript(snippet) ⇒ Object
Add snippet
to the request’s toplevel javascript cache.
The contents of this cache can be rendered into a <script type="text/javascript">
block by calling javascript_for_page
within the <head> of the layout.
27 28 29 30 |
# File 'lib/hammock/javascript_buffer.rb', line 27 def append_toplevel_javascript snippet @_toplevel_javascript ||= '' @_toplevel_javascript << snippet.strip.end_with(';') << "\n\n" unless snippet.nil? end |
#clear_js_caches ⇒ Object
57 58 59 |
# File 'lib/hammock/javascript_buffer.rb', line 57 def clear_js_caches @_domready_javascript = @_toplevel_javascript = nil end |
#javascript_for_ajax_response ⇒ Object
If the current request is XHR, render all cached javascript as javascript_for_page
would and clear the request’s javascript cache.
The purpose of this method is for rendering javascript into partials that form XHR responses, without causing duplicate javascript to be rendered by nested partials multiply calling this method.
48 49 50 51 52 53 54 55 |
# File 'lib/hammock/javascript_buffer.rb', line 48 def javascript_for_ajax_response # TODO this should be called from outside the partials somewhere, once only if request.xhr? js = javascript_for_page clear_js_caches js end end |
#javascript_for_page ⇒ Object
Render the snippets cached by append_javascript
and append_toplevel_javascript
within a <script type="text/javascript">
tag.
This should be called somewhere within the <head> in your layout.
35 36 37 38 39 40 41 42 43 |
# File 'lib/hammock/javascript_buffer.rb', line 35 def javascript_for_page javascript_tag %Q{ #{@_toplevel_javascript} (jQuery)(function() { #{@_domready_javascript} }); } end |