Added event-based observations when frequency is not set on observe_field/form #1474 [flash@vanklinkenbergsoftware.nl]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1532 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2005-06-27 05:29:47 +00:00
parent c19b74d9a6
commit de1f231c32
2 changed files with 18 additions and 5 deletions

@ -1,5 +1,7 @@
*SVN*
* Added event-based observations when frequency is not set on observe_field/form #1474 [flash@vanklinkenbergsoftware.nl]
* Added script.aculo.us Javascripts (controls.js, dragdrop.js, effects.js) (NEEDS MORE DESCRIPTION) #1509 [Thomas Fuchs]
* Fixed prototype to consider all fields it doesn't know as text (such as Safari's search) just like the browser in its serialization #1497 [Sean Treadway]

@ -176,12 +176,14 @@ def define_javascript_functions
# an Ajax call when its contents have changed.
#
# Required +options+ are:
# <tt>:frequency</tt>:: The frequency (in seconds) at which changes to
# this field will be detected.
# <tt>:url</tt>:: +url_for+-style options for the action to call
# when the field has changed.
#
# Additional options are:
# <tt>:frequency</tt>:: The frequency (in seconds) at which changes to
# this field will be detected. Set this to a value
# greater than zero to use time based observation
# instead of event based observation.
# <tt>:update</tt>:: Specifies the DOM ID of the element whose
# innerHTML should be updated with the
# XMLHttpRequest response text.
@ -193,7 +195,11 @@ def define_javascript_functions
# Additionally, you may specify any of the options documented in
# +link_to_remote.
def observe_field(field_id, options = {})
build_observer('Form.Element.Observer', field_id, options)
if options[:frequency]
build_observer('Form.Element.Observer', field_id, options)
else
build_observer('Form.Element.EventObserver', form_id, options)
end
end
# Like +observe_field+, but operates on an entire form identified by the
@ -201,7 +207,11 @@ def observe_field(field_id, options = {})
# the default value of the <tt>:with</tt> option evaluates to the
# serialized (request string) value of the form.
def observe_form(form_id, options = {})
build_observer('Form.Observer', form_id, options)
if options[:frequency]
build_observer('Form.Observer', form_id, options)
else
build_observer('Form.EventObserver', form_id, options)
end
end
@ -343,7 +353,8 @@ def build_observer(klass, name, options = {})
callback = remote_function(options)
javascript = '<script type="text/javascript">'
javascript << "new #{klass}('#{name}', "
javascript << "#{options[:frequency]}, function(element, value) {"
javascript << "#{options[:frequency]}, " if options[:frequency]
javascript << "function(element, value) {"
javascript << "#{callback}})</script>"
end