File/view/lib/helpers/ajax_helper.php

Description

Ajax helpers

Set of functions for working with javascript and Prototype functions, including functionality to call remote methods using Ajax.

Functions
auto_complete_field ($id, $options = array())
escape_javascript ($javascript)

Escape carrier returns and single and double quotes for javascript code

form_remote_tag ($options = array())

Returns a form tag that will submit using XMLHttpRequest in the background

in_place_editor ($field_id, $options = array())
in_place_editor_field ($object_name, $method, $object, $tag_options = array(), $editor_options = array())
javascript_tag ($code)

Returns a javascript tag with the $code inside

link_to_function ($content, $function, $html_options = array())

Returns a link that will trigger a javascript function using the onclick handler

Example :

link_to_function("Hello", "alert('Hello world !')");
 Produces:
    <a onclick="alert('Hello world !'); return false;" href="#">Hello</a>

link_to_remote ($content, $options = array(), $html_options = array())

Returns a link to a remote action whose url is defined by $options['url'].

This action is called using xmlHttpRequest and the response can be inserted into the page, in a DOM object whose id is specified by $options['update']. Usually, the response would be a partial prepared by the controller with either render_partial() or render_partial_collection().

Example :

link_to_remote("Delete this post", array('update' => 'posts',
     'url' => array('action' => 'destroy', 'id' => $this->post->id)));

You can also specify an array for $options['update'] to allow for easy redirection of output to an other DOM element if a server-side error occurs.

Example :

link_to_remote("Delete this post", array('url' => array('action' => 'destroy', 'id' => $this->post->id),
     'update' => array('success' => 'posts', 'failure' => 'errors')));

Optionally, you can use the $options['position'] parameter to influence how the target DOM element is updated. It must be one of 'before', 'top', 'bottom', or 'after'.

By default, these remote requests are processed asynchronous during which various JavaScript callbacks can be triggered (for progress indicators and the likes). All callbacks get access to the request object, which holds the underlying XMLHttpRequest. To access the server response, use request.responseText, to find out the HTTP status, use request.status.

The callbacks that may be specified are (in order):

  • 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.
  • success: Called when the XMLHttpRequest is completed, and the HTTP status code is in the 2XX range.
  • failure: Called when the XMLHttpRequest is completed, and the HTTP status code is not in the 2XX range.
  • complete: Called when the XMLHttpRequest is complete (fires after success/failure if they are present).

Example :

link_to_remote("Delete this post", array('url' => array('action' => 'destroy', 'id' => $this->post->id),
     'update'  => 'posts', 'failure' => "alert('HTTP Error ' + request.status + '!')"));

observe_field ($id, $options = array())

Observes the field with the DOM ID specified by $id and makes an Ajax call when its contents have changed.

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. 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.
  • 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.

observe_form ($id, $options = array())

Like observe_field, but operates on an entire form identified by the DOM ID $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.

remote_function ($options)

Returns the JavaScript needed for a remote function. Takes the same arguments as link_to_remote.

Example :

<select id="options" onchange="<?= remote_function(array('update' => "options", 'url' => array('action' => 'update_options'))); ? >">
   <option value="0">Hello</option>
   <option value="1">World</option>
 </select>

text_field_with_auto_complete ($object_name, $method, $object, $tag_options = array(), $completion_options = array())

Documentation generated on Wed, 24 Jan 2007 15:00:05 +0100 by phpDocumentor 1.3.0RC4