method
update_element_function
v1.0.0 -
Show latest stable
-
0 notes -
Class: ActionView::Helpers::JavaScriptHelper
- 1.0.0 (0)
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
update_element_function(element_id, options = {}, &block)
public
Returns a Javascript function (or expression) that’ll update a DOM element according to the options passed.
- :content: The content to use for updating. Can be left out if using block, see example.
- :action: Valid options are :update (assumed by default), :empty, :remove
- :position If the :action is :update, you can optionally specify one of the following positions: :before, :top, :bottom, :after.
Examples:
<%= javascript_tag(update_element_function( "products", :position => :bottom, :content => "<p>New product!</p>")) %> <% replacement_function = update_element_function("products") do %> <p>Product 1</p> <p>Product 2</p> <% end %> <%= javascript_tag(replacement_function) %>
This method can also be used in combination with remote method call where the result is evaluated afterwards to cause multiple updates on a page. Example:
# Calling view <%= form_remote_tag :url => { :action => "buy" }, :complete => evaluate_remote_response %> all the inputs here... # Controller action def buy @product = Product.find(1) end # Returning view <%= update_element_function( "cart", :action => :update, :position => :bottom, :content => "<p>New Product: #{@product.name}</p>")) %> <% update_element_function("status", :binding => binding) do %> You've bought a new product! <% end %>
Notice how the second call doesn’t need to be in an ERb output block since it uses a block and passes in the binding to render directly. This trick will however only work in ERb (not Builder or other template forms).