<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Prototype JavaScript framework - TimedObserver</title>
  <id>tag:www.prototypejs.org,2008:mephisto/api/timedObserver</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://www.prototypejs.org/feed/api/timedObserver/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.prototypejs.org/api/timedObserver" rel="alternate" type="text/html"/>
  <updated>2007-01-07T22:01:24Z</updated>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2007-01-07:12994</id>
    <published>2007-01-07T11:37:00Z</published>
    <updated>2007-01-07T22:01:24Z</updated>
    <category term="TimedObserver"/>
    <link href="http://www.prototypejs.org/api/timedObserver/form-element-observer" rel="alternate" type="text/html"/>
    <title>Form.Element.Observer</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Form.Element.Observer(element, frequency, callback)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A timed observer for a specific form control.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Form.Element.Observer(element, frequency, callback)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A timed observer for a specific form control.&lt;/p&gt;
&lt;p&gt;Form.Element observer implements the &lt;code&gt;getValue()&lt;/code&gt; method using &lt;a href=&quot;/api/form/element/getValue&quot;&gt;&lt;code&gt;Form.Element.getValue()&lt;/code&gt;&lt;/a&gt; on the given element.  See &lt;a href=&quot;../timedObserver&quot;&gt;&lt;code&gt;Abstract.TimedObserver&lt;/code&gt;&lt;/a&gt; for general documentation on timed observers.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2007-01-07:12992</id>
    <published>2007-01-07T09:53:00Z</published>
    <updated>2007-01-07T22:02:23Z</updated>
    <category term="TimedObserver"/>
    <link href="http://www.prototypejs.org/api/timedObserver/form-observer" rel="alternate" type="text/html"/>
    <title>Form.Observer</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Form.Observer(element, frequency, callback)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A timed observer that triggers when any value changes within the form.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Form.Observer(element, frequency, callback)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A timed observer that triggers when any value changes within the form.&lt;/p&gt;
&lt;p&gt;Form observer implements the &lt;code&gt;getValue()&lt;/code&gt; method using &lt;a href=&quot;/api/form/serialize&quot;&gt;&lt;code&gt;Form.serialize()&lt;/code&gt;&lt;/a&gt; on the element from the first argument. See &lt;a href=&quot;../timedObserver&quot;&gt;&lt;code&gt;Abstract.TimedObserver&lt;/code&gt;&lt;/a&gt; for general documentation on timed observers.&lt;/p&gt;

&lt;h3&gt;Example&lt;/h3&gt;

&lt;p&gt;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).&lt;/p&gt;

&amp;lt;form class=&quot;example&quot; action=&quot;#&quot; id=&quot;example&quot;&gt;
  &lt;fieldset&gt;
    &lt;legend&gt;Login Preferences&lt;/legend&gt;
    &lt;p class=&quot;message&quot;&gt;Current settings:&lt;/p&gt;
    &lt;div&gt;
      &amp;lt;label for=&quot;greeting&quot;&gt;Greeting message&amp;lt;/label&gt;
      &amp;lt;input name=&quot;greeting&quot; type=&quot;text&quot; id=&quot;greeting&quot; value=&quot;Hello world!&quot; /&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;span class=&quot;label&quot;&gt;Login options&lt;/span&gt;
      &lt;ul&gt;
        &lt;li&gt;&amp;lt;input checked=&quot;checked&quot; name=&quot;login-visible&quot; type=&quot;checkbox&quot; id=&quot;login-visible&quot; /&gt;
          &amp;lt;label for=&quot;login-visible&quot;&gt;allow others to see my last login date&amp;lt;/label&gt;&lt;/li&gt;
        &lt;li&gt;&amp;lt;input name=&quot;land-recent&quot; type=&quot;checkbox&quot; id=&quot;land-recent&quot; /&gt;
          &amp;lt;label for=&quot;land-recent&quot;&gt;land on recent changes overview instead of the Dashboard&amp;lt;/label&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div class=&quot;buttonrow&quot;&gt;&amp;lt;input type=&quot;submit&quot; value=&quot;save&quot; /&gt;&lt;/div&gt;
  &lt;/fieldset&gt;

  &amp;lt;/form&gt;

&lt;p&gt;Here is the complete JavaScript code for the above example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
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
}
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Mislav</name>
    </author>
    <id>tag:www.prototypejs.org,2007-01-07:12991</id>
    <published>2007-01-07T09:51:00Z</published>
    <updated>2007-01-07T11:52:34Z</updated>
    <category term="TimedObserver"/>
    <link href="http://www.prototypejs.org/api/timedObserver" rel="alternate" type="text/html"/>
    <title>TimedObserver</title>
<content type="html">
            &lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;TimedObserver&lt;/code&gt; object will try to check some value using the &lt;code&gt;getValue()&lt;/code&gt; instance method which isn't defined in this class. You must use the concrete implementations of &lt;code&gt;TimedObserver&lt;/code&gt; like &lt;code&gt;Form.Observer&lt;/code&gt; or &lt;code&gt;Form.Element.Observer&lt;/code&gt;. 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.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;TimedObserver&lt;/code&gt; implementations is straightforward; simply instantiate them with appropriate arguments. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
new Form.Element.Observer(
  'myelement',
  0.2,  // 200 milliseconds
  function(el, value){
    alert('The form control has changed value to: ' + value)
  }
)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;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).&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;
          </content>  </entry>
</feed>
