diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-18 21:27:05 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-05-18 21:27:05 +0000 |
commit | 4221494348f06b0c44446a44571c8ad8b74e1da6 (patch) | |
tree | 647820e2c4ddf97ee6eea359dec26dab56c97a2c | |
parent | c597e7d9ac82a1c393c645fce06f615ed5abba87 (diff) | |
download | rails-4221494348f06b0c44446a44571c8ad8b74e1da6.tar.gz rails-4221494348f06b0c44446a44571c8ad8b74e1da6.tar.bz2 rails-4221494348f06b0c44446a44571c8ad8b74e1da6.zip |
observe_form always sends the serialized form. Closes #5271.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6775 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/prototype_helper.rb | 50 | ||||
-rw-r--r-- | actionpack/test/template/prototype_helper_test.rb | 4 |
3 files changed, 37 insertions, 19 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 6e58793ffd..c537a23d19 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* observe_form always sends the serialized form. #5271 [manfred, normelton@gmail.com] + * Parse url-encoded and multipart requests ourselves instead of delegating to CGI. [Jeremy Kemper] * select :include_blank option can be set to a string instead of true, which just uses an empty string. #7664 [Wizard] diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 1586a99e4c..8c3cd907ff 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -261,8 +261,10 @@ module ActionView 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 @@ -279,14 +281,24 @@ module ActionView # <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, @@ -302,11 +314,15 @@ module ActionView 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) @@ -682,10 +698,10 @@ module ActionView 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) diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 6158584e9d..a94802fdfb 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -170,7 +170,7 @@ class PrototypeHelperTest < Test::Unit::TestCase 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 @@ -180,7 +180,7 @@ class PrototypeHelperTest < Test::Unit::TestCase 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 |