observe_field
- 1.0.0
- 1.1.6 (0)
- 1.2.6 (5)
- 2.0.3 (17)
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (-6)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
observe_field(field_id, options = {})
public
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 when the field has changed. |
:function: | Instead of making a remote call to a URL, you can specify a function to be called instead. |
Additional options are:
:frequency: | The frequency (in seconds) at which changes to this field will be detected. Not setting this option at all or to a value equal to or less than zero will use event based observation instead of time based observation. |
: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.
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, you can specify it instead to be "blur" or "focus" or any other event. |
Additionally, you may specify any of the options documented in link_to_remote.
Pass the observed fields value as a parameter in the Ajax request
Use encodeURIComponent with with to pass an encoded value as a parameter (POST or GET) of the AJAX request. For example:
<%= observe_field :company_id,
:url => {:action => ‘facilities’, :only_path => false}, :with => “‘company=’ + encodeURIComponent(value)” %> Also, setting only_path => false for the URL ensures that the full URL (including host and protocol) is used for the AJAX request.
Common options
“Common options” mentioned here is default PrototypeHelper options documented in link_to_remote
This means you can use :loading, :loaded, :failure, :success, etc in observe_field.
CAUTION! :frequency option description is misleading
To use event-based observer, don’t supply :frequency param at all. :frequency => 0 causes JS error.
Use this option only if time-based observer is what you need.