aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/helpers/prototype_helper.rb6
-rw-r--r--actionpack/test/template/prototype_helper_test.rb10
3 files changed, 16 insertions, 2 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 4024be84c1..5d9c228007 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added :function option to PrototypeHelper#observe_field/observe_form that allows you to call a function instead of submitting an ajax call as the trigger #4268 [jonathan@daikini.com]
+
* Make Mime::Type.parse consider q values (if any) [Jamis Buck]
* XML-formatted requests are typecast according to "type" attributes for :xml_simple [Jamis Buck]
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb
index d49bc94517..35a671c081 100644
--- a/actionpack/lib/action_view/helpers/prototype_helper.rb
+++ b/actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -315,9 +315,11 @@ module ActionView
# Observes the field with the DOM ID specified by +field_id+ and makes
# an Ajax call when its contents have changed.
#
- # Required +options+ are:
+ # Required +options+ are either of:
# <tt>:url</tt>:: +url_for+-style options for the action to call
# when the field has changed.
+ # <tt>:function</tt>:: Instead of making a remote call to a URL, you
+ # can specify a function to be called instead.
#
# Additional options are:
# <tt>:frequency</tt>:: The frequency (in seconds) at which changes to
@@ -702,7 +704,7 @@ module ActionView
options[:with] ||= 'value' if options[:update]
end
- callback = remote_function(options)
+ callback = options[:function] || remote_function(options)
javascript = "new #{klass}('#{name}', "
javascript << "#{options[:frequency]}, " if options[:frequency]
javascript << "function(element, value) {"
diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb
index e0746f62e6..eb8de1ff40 100644
--- a/actionpack/test/template/prototype_helper_test.rb
+++ b/actionpack/test/template/prototype_helper_test.rb
@@ -108,10 +108,20 @@ class PrototypeHelperTest < Test::Unit::TestCase
observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" })
end
+ def test_observe_field_using_function_for_callback
+ assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {alert('Element changed')})\n//]]>\n</script>),
+ observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')")
+ 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>),
observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" })
end
+
+ def test_observe_form_using_function_for_callback
+ assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {alert('Form changed')})\n//]]>\n</script>),
+ observe_form("cart", :frequency => 2, :function => "alert('Form changed')")
+ end
def test_update_element_function
assert_equal %($('myelement').innerHTML = 'blub';\n),