Skip to content

Commit

Permalink
Merge [6775] to stable from trunk: observe_form always sends the seri…
Browse files Browse the repository at this point in the history
…alized form. References #5271.

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@6776 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed May 18, 2007
1 parent 173e2d2 commit d4c24b6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* observe_form always sends the serialized form. #5271 [manfred, normelton@gmail.com]

* Update UrlWriter to accept :anchor parameter. Closes #6771. [octopod]

* Replace the current block/continuation filter chain handling by an implementation based on a simple loop. Closes #8226 [Stefan Kaes]
Expand Down
50 changes: 33 additions & 17 deletions actionpack/lib/action_view/helpers/prototype_helper.rb
Expand Up @@ -250,8 +250,10 @@ def remote_function(options)
return function
end

# Observes the field with the DOM ID specified by +field_id+ and makes
# an Ajax call when its contents have changed.
# Observes the field with the DOM ID specified by +field_id+ and calls a
# callback when its contents have changed. The default callback is an
# Ajax call. By default the value of the observed field is sent as a
# parameter with the Ajax call.
#
# Required +options+ are either of:
# <tt>:url</tt>:: +url_for+-style options for the action to call
Expand All @@ -268,14 +270,24 @@ def remote_function(options)
# <tt>:update</tt>:: Specifies the DOM ID of the element whose
# innerHTML should be updated with the
# XMLHttpRequest response text.
# <tt>:with</tt>:: A JavaScript expression specifying the
# parameters for the XMLHttpRequest. This defaults
# to 'value', which in the evaluated context
# refers to the new field value. If you specify a
# string without a "=", it'll be extended to mean
# the form key that the value should be assigned to.
# So :with => "term" gives "'term'=value". If a "=" is
# present, no extension will happen.
# <tt>:with</tt>:: A JavaScript expression specifying the parameters
# for the XMLHttpRequest. The default is to send the
# key and value of the observed field. Any custom
# expressions should return a valid URL query string.
# The value of the field is stored in the JavaScript
# variable +value+.
#
# Examples
#
# :with => "'my_custom_key=' + value"
# :with => "'person[name]=' + prompt('New name')"
# :with => "Form.Element.serialize('other-field')"
#
# Finally
# :with => 'name'
# is shorthand for
# :with => "'name=' + value"
# This essentially just changes the key of the parameter.
# <tt>:on</tt>:: Specifies which event handler to observe. By default,
# it's set to "changed" for text fields and areas and
# "click" for radio buttons and checkboxes. With this,
Expand All @@ -291,11 +303,15 @@ def observe_field(field_id, options = {})
build_observer('Form.Element.EventObserver', field_id, options)
end
end

# Like +observe_field+, but operates on an entire form identified by the
# DOM ID +form_id+. +options+ are the same as +observe_field+, except
# the default value of the <tt>:with</tt> option evaluates to the
# serialized (request string) value of the form.

# Observes the form with the DOM ID specified by +form_id+ and calls a
# callback when its contents have changed. The default callback is an
# Ajax call. By default all fields of the observed field are sent as
# parameters with the Ajax call.
#
# The +options+ for +observe_form+ are the same as the options for
# +observe_field+. The JavaScript variable +value+ available to the
# <tt>:with</tt> option is set to the serialized form by default.
def observe_form(form_id, options = {})
if options[:frequency]
build_observer('Form.Observer', form_id, options)
Expand Down Expand Up @@ -660,10 +676,10 @@ def method_option_to_s(method)
end

def build_observer(klass, name, options = {})
if options[:with] && !options[:with].include?("=")
if options[:with] && (options[:with] !~ /[=(.]/)
options[:with] = "'#{options[:with]}=' + value"
else
options[:with] ||= 'value' if options[:update]
options[:with] ||= 'value' unless options[:function]
end

callback = options[:function] || remote_function(options)
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/template/prototype_helper_test.rb
Expand Up @@ -125,7 +125,7 @@ def test_submit_to_remote
end

def test_observe_field
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/reorder_if_empty', {asynchronous:true, evalScripts:true})})\n//]]>\n</script>),
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/reorder_if_empty', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" })
end

Expand All @@ -135,7 +135,7 @@ def test_observe_field_using_function_for_callback
end

def test_observe_form
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {new Ajax.Request('http://www.example.com/cart_changed', {asynchronous:true, evalScripts:true})})\n//]]>\n</script>),
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {new Ajax.Request('http://www.example.com/cart_changed', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" })
end

Expand Down

0 comments on commit d4c24b6

Please sign in to comment.