Prototype JavaScript framework - TimedObserver tag:www.prototypejs.org,2011:mephisto/api/timedObserver Mephisto Noh-Varr 2007-01-07T22:01:24Z Mislav tag:www.prototypejs.org,2007-01-07:12994 2007-01-07T11:37:00Z 2007-01-07T22:01:24Z Form.Element.Observer <pre><code class="ebnf">new Form.Element.Observer(element, frequency, callback)</code></pre> <p>A timed observer for a specific form control.</p> <pre><code class="ebnf">new Form.Element.Observer(element, frequency, callback)</code></pre> <p>A timed observer for a specific form control.</p> <p>Form.Element observer implements the <code>getValue()</code> method using <a href="/api/form/element/getValue"><code>Form.Element.getValue()</code></a> on the given element. See <a href="../timedObserver"><code>Abstract.TimedObserver</code></a> for general documentation on timed observers.</p> Mislav tag:www.prototypejs.org,2007-01-07:12992 2007-01-07T09:53:00Z 2007-01-07T22:02:23Z Form.Observer <pre><code class="ebnf">new Form.Observer(element, frequency, callback)</code></pre> <p>A timed observer that triggers when any value changes within the form.</p> <pre><code class="ebnf">new Form.Observer(element, frequency, callback)</code></pre> <p>A timed observer that triggers when any value changes within the form.</p> <p>Form observer implements the <code>getValue()</code> method using <a href="/api/form/serialize"><code>Form.serialize()</code></a> on the element from the first argument. See <a href="../timedObserver"><code>Abstract.TimedObserver</code></a> for general documentation on timed observers.</p> <h3>Example</h3> <p>In this example an observer is used to change the appearance of the form if any of the values had been changed. It returns to its initial state when the data is submitted (saved).</p> &lt;form class="example" action="#" id="example"> <fieldset> <legend>Login Preferences</legend> <p class="message">Current settings:</p> <div> &lt;label for="greeting">Greeting message&lt;/label> &lt;input name="greeting" type="text" id="greeting" value="Hello world!" /> </div> <div> <span class="label">Login options</span> <ul> <li>&lt;input checked="checked" name="login-visible" type="checkbox" id="login-visible" /> &lt;label for="login-visible">allow others to see my last login date&lt;/label></li> <li>&lt;input name="land-recent" type="checkbox" id="land-recent" /> &lt;label for="land-recent">land on recent changes overview instead of the Dashboard&lt;/label></li> </ul> </div> <div class="buttonrow">&lt;input type="submit" value="save" /></div> </fieldset> &lt;/form> <p>Here is the complete JavaScript code for the above example:</p> <pre><code class="javascript"> new Form.Observer('example', 0.3, function(form, value){ $('msg').update('Your preferences have changed. Resubmit to save').style.color = 'red' form.down().setStyle({ background:'lemonchiffon', borderColor:'red' }) }) $('example').onsubmit = function() { $('msg').update('Preferences saved!').style.color = 'green' this.down().setStyle({ background:null, borderColor:null }) return false } </code></pre> Mislav tag:www.prototypejs.org,2007-01-07:12991 2007-01-07T09:51:00Z 2007-01-07T11:52:34Z TimedObserver <p>An abstract observer class which instances can be used to periodically check some value and trigger a callback when the value has changed. The frequency is in seconds.</p> <p>A <code>TimedObserver</code> object will try to check some value using the <code>getValue()</code> instance method which isn't defined in this class. You must use the concrete implementations of <code>TimedObserver</code> like <code>Form.Observer</code> or <code>Form.Element.Observer</code>. The former serializes a form and triggers when the result has changed, while the latter simply triggers when the value of a certain form control changes.</p> <p>Using <code>TimedObserver</code> implementations is straightforward; simply instantiate them with appropriate arguments. For example:</p> <pre><code class="javascript"> new Form.Element.Observer( 'myelement', 0.2, // 200 milliseconds function(el, value){ alert('The form control has changed value to: ' + value) } ) </code></pre> <p>Now that we have instantiated an object, it will check the value of the form control every 0.2 seconds and alert us of any change. While it is useless to alert the user of his own input (like in the example), we could be doing something useful like updating a certain part of the UI or informing the application on server of stuff happening (over Ajax).</p> <p>The callback function is always called with 2 arguments: the element given when the observer instance was made and the actual value that has changed and caused the callback to be triggered in the first place.</p>