Provides a set of helpers for calling Javascript functions and, most importantly, to call remote methods using what has been labelled Ajax. This means that you can call actions in your controllers without reloading the page, but still update certain parts of it using injections into the DOM. The common use case is having a form that adds a new element to a list without reloading the page.
To be able to use the Javascript helpers, you must either call <%= define_javascript_functions %> (which returns all the Javascript support functions in a <script> block) or reference the Javascript library using <%= javascript_include_tag "prototype" %> (which looks for the library in /javascripts/prototype.js). The latter is recommended as the browser can then cache the library instead of fetching all the functions anew on every request.
If you’re the visual type, there’s an Ajax movie demonstrating the use of form_remote_tag.
- define_javascript_functions
- escape_javascript
- form_remote_tag
- link_to_function
- link_to_remote
- observe_field
- observe_form
- periodically_call_remote
- submit_to_remote
| CALLBACKS | = | [:uninitialized, :loading, :loaded, :interactive, :complete] |
| JAVASCRIPT_PATH | = | File.join(File.dirname(__FILE__), 'javascripts') |
Includes the Action Pack Javascript library inside a single <script> tag.
Note: The recommended approach is to copy the contents of lib/action_view/helpers/javascripts/ into your application’s public/javascripts/ directory, and use javascript_include_tag to create remote <script> links.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/javascript_helper.rb, line 135
135: def define_javascript_functions
136: javascript = '<script type="text/javascript">'
137: Dir.glob(File.join(JAVASCRIPT_PATH, '*')).each { |filename| javascript << "\n" << IO.read(filename) }
138: javascript << '</script>'
139: end
Escape carrier returns and single and double quotes for Javascript segments.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/javascript_helper.rb, line 174
174: def escape_javascript(javascript)
175: (javascript || '').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
176: end
Returns a form tag that will submit using XMLHttpRequest in the background instead of the regular reloading POST arrangement. Even though it’s using Javascript to serialize the form elements, the form submission will work just like a regular submission as viewed by the receiving side (all elements available in @params). The options for specifying the target with :url and defining callbacks is the same as link_to_remote.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/javascript_helper.rb, line 87
87: def form_remote_tag(options = {})
88: options[:form] = true
89:
90: options[:html] ||= {}
91: options[:html][:onsubmit] = "#{remote_function(options)}; return false;"
92:
93: tag("form", options[:html], true)
94: end
Returns a link that’ll trigger a javascript function using the onclick handler and return false after the fact.
Examples:
link_to_function "Greeting", "alert('Hello world!')"
link_to_function(image_tag("delete"), "if confirm('Really?'){ do_delete(); }")
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/javascript_helper.rb, line 29
29: def link_to_function(name, function, html_options = {})
30: content_tag(
31: "a", name,
32: {:href => "#", :onclick => "#{function}; return false;"}.merge(html_options.symbolize_keys)
33: )
34: end
Returns a link to a remote action defined by options[:url] (using the url_for format) that’s called in the background using XMLHttpRequest. The result of that request can then be inserted into a DOM object whose id can be specified with options[:update]. Usually, the result would be a partial prepared by the controller with either render_partial or render_partial_collection.
Examples:
link_to_remote "Delete this post", :update => "posts", :url => { :action => "destroy", :id => post.id }
link_to_remote(image_tag("refresh"), :update => "emails", :url => { :action => "list_emails" })
By default, these remote requests are processed asynchronous during which various callbacks can be triggered (for progress indicators and the likes).
Example:
link_to_remote word,
:url => { :action => "undo", :n => word_counter },
:complete => "undoRequestCompleted(request)"
The callbacks that may be specified are:
| :loading: | Called when the remote document is being loaded with data by the browser. |
| :loaded: | Called when the browser has finished loading the remote document. |
| :interactive: | Called when the user can interact with the remote document, even though it has not finished loading. |
| :complete: | Called when the XMLHttpRequest is complete. |
If you for some reason or another need synchronous processing (that’ll block the browser while the request is happening), you can specify options[:type] = :synchronous.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/javascript_helper.rb, line 70
70: def link_to_remote(name, options = {}, html_options = {})
71: link_to_function(name, remote_function(options), html_options)
72: end
Observes the field with the DOM ID specified by field_id and makes an Ajax when its contents have changed.
Required options are:
| :frequency: | The frequency (in seconds) at which changes to this field will be detected. |
| :url: | url_for-style options for the action to call when the field has changed. |
Additional options are:
| :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. |
Additionally, you may specify any of the options documented in +link_to_remote.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/javascript_helper.rb, line 161
161: def observe_field(field_id, options = {})
162: build_observer('Form.Element.Observer', field_id, options)
163: 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.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/javascript_helper.rb, line 169
169: def observe_form(form_id, options = {})
170: build_observer('Form.Observer', form_id, options)
171: end
Periodically calls the specified url (options[:url]) every options[:frequency] seconds (default is 10). Usually used to update a specified div (options[:update]) with the results of the remote call. The options for specifying the target with :url and defining callbacks is the same as link_to_remote.
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/javascript_helper.rb, line 77
77: def periodically_call_remote(options = {})
78: frequency = options[:frequency] || 10 # every ten seconds by default
79: code = "new PeriodicalExecuter(function() {#{remote_function(options)}}, #{frequency})"
80: content_tag("script", code, options[:html_options] || {})
81: end
Returns a button input tag that will submit form using XMLHttpRequest in the background instead of regular reloading POST arrangement. options argument is the same as in form_remote_tag
[ show source ]
# File vendor/rails/actionpack/lib/action_view/helpers/javascript_helper.rb, line 98
98: def submit_to_remote(name, value, options = {})
99: options[:with] = 'Form.serialize(this.form)'
100:
101: options[:html] ||= {}
102: options[:html][:type] = 'button'
103: options[:html][:onclick] = "#{remote_function(options)}; return false;"
104: options[:html][:name] = name
105: options[:html][:value] = value
106:
107: tag("input", options[:html], false)
108: end