No commit activity in last 3 years
No release in over 3 years
Delayed Form Observer provides Rails with timed form observers that do not create race conditions. The gem extends Prototype and adds a Rails PrototypeHelper to use this new object.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 2.3.4
 Project Readme

Delayed Form Observer¶ ↑

Delayed Form Observer mitigates a race condition in a common Rails search implementation.

Large lists are often made more manageable by positioning a search textbox above the list and attaching a Prototype timed observer to the textbox. A javascript object is checking the textbox for changes and remotely submitting the search form every so many milliseconds. The user experiences this as a list whose items change with every few keystrokes.

The pattern works fine as long as the search query is working against a simple dataset. The asynchronous requests come back in the same order they are sent.

But if the dataset is not simple, search #1 might return after search #2. A user who types in “J” then “o” initiates two searches against a list of contacts. “J” matches both Jim and John; “Jo” only matches John. But since it is easier for the database to return fewer records, what if we get [John] before we get [Jim,John]? The textbox will show “Jo” but the list will show Jim and John. Oops.

Delayed Form Observer eliminates this condition for all common applications by ignoring form changes until the user stops updating the form contents for a cycle equal to the observation frequency. In practical terms, this means the form observer will only make one remote request for a contiguous sequence of keystrokes.

Usage (Rails 3)¶ ↑

The generator that comes with this gem adds the required Prototype extension to your application’s public javascripts directory. Just hook up the observer using unobtrusive javascript.

// Make search form observer
window.observe("load", function() {
    var form =  $('my_search_form');
    var fo   =  new Form.DelayedObserver(
                    form, 
                    0.2,
                    function(element, value) {
                        new Ajax.Request(
                            form.action, 
                            { 
                                asynchronous: true, 
                                evalScripts:  true, 
                                method:       "get",
                                parameters:   value
                            }
                        );
                    }
    );
});

Usage (Rails 2)¶ ↑

The generator that comes with this gem adds the required Prototype extension to your application’s public javascripts directory. It also adds an ActionView Prototype helper for creating a form observer that uses this new Prototype object.

The helper uses the same arguments as observe_form.

# Observe changes to the search form
<%= delayed_observe_form "MySearchForm", :url => { :action => :index }, :method => :get, :frequency => 0.5 %>

Prerequisites¶ ↑

As you might have guessed, a gem that extends Prototype requires Prototype.

But since the gem was designed as a Rails extension, chances are Prototype is already included.

Installation & Generators (Rails 3)¶ ↑

Install me from RubyGems.org by adding a gem dependency to your Gemfile. Bundler does the rest.

gem “delayed_form_observer” $ bundle install

Then generate the required javascript file.

$ rails g delayed_form_observer

Installation & Generators (Rails 2)¶ ↑

Install as a gem from RubyGems.org and add a gem dependency in the appropriate file.

$ gem install delayed_form_observer

Or install as a plugin.

$ script/plugin install git://github.com/coroutine/delayed_form_observer.git

Either way, then generate the required javascript file.

$ script/generate delayed_form_observer

Gemroll¶ ↑

Other gems by Coroutine include:

License¶ ↑

Copyright © 2010 Coroutine LLC.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.