From 4221494348f06b0c44446a44571c8ad8b74e1da6 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 18 May 2007 21:27:05 +0000 Subject: 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 --- .../lib/action_view/helpers/prototype_helper.rb | 50 ++++++++++++++-------- 1 file changed, 33 insertions(+), 17 deletions(-) (limited to 'actionpack/lib/action_view') 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: # :url:: +url_for+-style options for the action to call @@ -279,14 +281,24 @@ module ActionView # :update:: Specifies the DOM ID of the element whose # innerHTML should be updated with the # XMLHttpRequest response text. - # :with:: 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. + # :with:: 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. # :on:: 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 :with 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 + # :with 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) -- cgit v1.2.3