Hello Augmenters,

(lighten up! ;-) )


[post order restored]
On 12/4/06, Thomas Fuchs <[EMAIL PROTECTED]> wrote:
> Am 01.12.2006 um 19:15 schrieb Mislav:
>
> > Just don't whine - make it work.
> > (And contribute.)
>
> Exactly.

Ok. Here's a suggestion.

Prototype's abstract timed observer code looks a little bulky.

Here is my implementation of a stand alone form input observer. I
think el for element, hz for frequency, cb for callback, nv for new
value, and lv for last value are not extreme abbreviations.

Form.Element.Observer = function(el, hz, cb) {
  el=$(el);
  var lv; // optionally could be  var lv = el.value;  or  var lv = '';
  setInterval(function() {
                 var nv = el.value;
                 if (nv !== lv) {
                   cb(el, nv);
                   lv = nv;
                 }
               },
               hz*1e3);
};

Below is the above code minimized.

Form.Element.Observer=function(el,hz,cb){el=$(el);var
lv;setInterval(function(){var
nv=el.value;if(nv!==lv){cb(el,nv);lv=nv;}},hz*1e3);};


Below is the code in Prototype necessary just to extend the existing
bulk of the abstract timed observer.

Form.Element.Observer=Class.create();Form.Element.Observer.prototype=Object.extend(new
Abstract.TimedObserver(),{getValue:function(){return
Form.Element.getValue(this.element);}});


Note that my code to implement an entire form element observer is
shorter than the code to extend the abstract timed observer even if
getValue and element are abbreviated to two letters. So Prototype's
use of object inheritance is encouraging bulkier code and there is the
overhead of the abstract observer. Although I didn't make a
performance test, I'm quite sure the shorter version will also run
faster because there are less function calls and object property
lookups.

Note that in the shorter version there is no need for the for the
complexity of Class.create() and Object.extend(). And to start a timed
observer a new object doesn't have to be instantiated. The Prototype
code doesn't look like it really allows for any use of this object
anyway. If I've missed something here then that changes everything.
However without retaining the value returned by setInterval() I can't
see that much can be manipulated after the observer starts. You could
play weird tricks like changing the callback function but I doubt
anyone does that and it wouldn't fall under the 80/20 rule.

If my suggested code was used there would be less Prototype for web
users to download and less code for the Prototype community to
maintain.


Note also that in the Prototype code

if (this.lastValue != value) {

should probably be

if (this.lastValue !== value) {

because both 0 and the empty string '' typecast to false and so 0!=''
returns false.



Note also that a semicolon at the end of this line looks like it would
make the observer section minimizable

Abstract.TimedObserver = function() {}



I know this group is very sensitive to technical criticism of
Prototype but I hope this is received well.


Peter
-----
http://forkjavascript.org

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to