[Proto-Scripty] Re: Is there a way to DRY this up?

2010-12-23 Thread Neaox
Like this?:

function filterHandler(evt){this.fire('check:filter');}
$('filter').observe('keyup',
filterHandler).observe('click',filterHandler).observe('focus',
filterHandler).observe('blur', filterHandler);

See Observe Documentation: http://www.prototypejs.org/api/event/observe

As the Event.Observe function returns the element the event listener
is being applied too you can just call 'observe' in tandem. I also
created a function named 'filterHandler' that each event listener will
call as they all run the same code, this makes it easier if you wish
to make a change to the function and removes the (assumed) needless
repetition, however if you intend to call different functions with
each event listener use the code below:

$('filter').observe('keyup', function(evt)
{this.fire('check:filter');}).observe('click',function(evt)
{this.fire('check:filter');}).observe('focus', function(evt)
{this.fire('check:filter');}).observe('blur', function(evt)
{this.fire('check:filter');});

Hope this helps,

Chris


On Dec 23, 7:16 am, Walter Lee Davis wa...@wdstudio.com wrote:
 I have a quick filter for hiding list items until only matches show. I
 want to cover all the various ways that a user might interact with the
 search field, so I write this lovely:

 $('filter').observe('keyup', function(evt){
 this.fire('check:filter');
 });
 $('filter').observe('click', function(evt){
 this.fire('check:filter');
 });
 $('filter').observe('focus', function(evt){
 this.fire('check:filter');
 });
 $('filter').observe('blur', function(evt){
 this.fire('check:filter');
 });

 Is there any way to write this more clearly, as in with fewer lines of
 code?

 I'm using 1.6.latest, haven't tried the new 1.7 goodies yet. Would
 that help?

 Walter

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



[Proto-Scripty] Re: Is there a way to DRY this up?

2010-12-23 Thread T.J. Crowder
@Richard:

 Maybe something like ...

 ['keyup', 'click', 'focus', 'blur'].each(function(eventName){
  $('filter').observe(eventName, function(evt){
   this.fire('check:filter');
  });

 });

That creates four identical functions (one for each event). Not
necessarily a problem, but...

@Walter: How 'bout *really* not repeating yourself: ;-)

// In your bag of tricks
Element.addMethods({
observeAll: function(element, eventNames, handler) {
if (!(element = $(element))) return;
eventNames = typeof eventNames === string ? $w(eventNames) :
eventNames;
eventNames.each(function(eventName) {
Event.observe(element, eventName, handler);
});
return element;
}
});

// Then in this specific case:
$('filter').observeAll(['keyup', 'click', 'focus', 'blur'],
function(evt) {
  this.fire('check:filter');
});

// Or:
$('filter').observeAll('keyup click focus blur', function(evt) {
  this.fire('check:filter');
});

// Or even:
var filter = $('filter');
filter.observeAll(
   'keyup click focus blur',
   Element.fire.curry(filter, 'check:filter'));

FWIW,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On Dec 23, 10:02 am, Richard Quadling rquadl...@gmail.com wrote:
 On 22 December 2010 18:16, Walter Lee Davis wa...@wdstudio.com wrote:









  I have a quick filter for hiding list items until only matches show. I want
  to cover all the various ways that a user might interact with the search
  field, so I write this lovely:

         $('filter').observe('keyup', function(evt){
                 this.fire('check:filter');
         });
         $('filter').observe('click', function(evt){
                 this.fire('check:filter');
         });
         $('filter').observe('focus', function(evt){
                 this.fire('check:filter');
         });
         $('filter').observe('blur', function(evt){
                 this.fire('check:filter');
         });

  Is there any way to write this more clearly, as in with fewer lines of code?

  I'm using 1.6.latest, haven't tried the new 1.7 goodies yet. Would that
  help?

  Walter

  --
  You received this message because you are subscribed to the Google Groups
  Prototype  script.aculo.us group.
  To post to this group, send email to
  prototype-scriptacul...@googlegroups.com.
  To unsubscribe from this group, send email to
  prototype-scriptaculous+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/prototype-scriptaculous?hl=en.

 Maybe something like ...

 ['keyup', 'click', 'focus', 'blur'].each(function(eventName){
  $('filter').observe(eventName, function(evt){
   this.fire('check:filter');
  });

 });

 --
 Richard Quadling
 Twitter : EE : Zend
 @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



Re: [Proto-Scripty] Re: Is there a way to DRY this up?

2010-12-23 Thread Walter Lee Davis

As usual, taking it to the space elevator! Thanks so much!

Walter

On Dec 23, 2010, at 10:21 AM, T.J. Crowder wrote:


@Richard:


Maybe something like ...

['keyup', 'click', 'focus', 'blur'].each(function(eventName){
 $('filter').observe(eventName, function(evt){
  this.fire('check:filter');
 });

});


That creates four identical functions (one for each event). Not
necessarily a problem, but...

@Walter: How 'bout *really* not repeating yourself: ;-)

// In your bag of tricks
Element.addMethods({
   observeAll: function(element, eventNames, handler) {
   if (!(element = $(element))) return;
   eventNames = typeof eventNames === string ? $w(eventNames) :
eventNames;
   eventNames.each(function(eventName) {
   Event.observe(element, eventName, handler);
   });
   return element;
   }
});

// Then in this specific case:
$('filter').observeAll(['keyup', 'click', 'focus', 'blur'],
function(evt) {
  this.fire('check:filter');
});

// Or:
$('filter').observeAll('keyup click focus blur', function(evt) {
  this.fire('check:filter');
});

// Or even:
var filter = $('filter');
filter.observeAll(
  'keyup click focus blur',
  Element.fire.curry(filter, 'check:filter'));

FWIW,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On Dec 23, 10:02 am, Richard Quadling rquadl...@gmail.com wrote:
On 22 December 2010 18:16, Walter Lee Davis wa...@wdstudio.com  
wrote:










I have a quick filter for hiding list items until only matches  
show. I want
to cover all the various ways that a user might interact with the  
search

field, so I write this lovely:



   $('filter').observe('keyup', function(evt){
   this.fire('check:filter');
   });
   $('filter').observe('click', function(evt){
   this.fire('check:filter');
   });
   $('filter').observe('focus', function(evt){
   this.fire('check:filter');
   });
   $('filter').observe('blur', function(evt){
   this.fire('check:filter');
   });


Is there any way to write this more clearly, as in with fewer  
lines of code?


I'm using 1.6.latest, haven't tried the new 1.7 goodies yet. Would  
that

help?



Walter



--
You received this message because you are subscribed to the Google  
Groups

Prototype  script.aculo.us group.
To post to this group, send email to
prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en.


Maybe something like ...

['keyup', 'click', 'focus', 'blur'].each(function(eventName){
 $('filter').observe(eventName, function(evt){
  this.fire('check:filter');
 });

});

--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY


--
You received this message because you are subscribed to the Google  
Groups Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com 
.
To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com 
.
For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en 
.




--
You received this message because you are subscribed to the Google Groups Prototype 
 script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.



Re: [Proto-Scripty] Re: Is there a way to DRY this up?

2010-12-23 Thread Richard Quadling
@TJ

Yeah. I sort of noticed that.

I like the observeAll mechanism. Very reusable.

In PHP, I could ...

if (!!fn || fn = function(){...}) {
 fn();
}

sort of thing. Basically assign a value to a variable and evaluate the
assignment. For closures, this is always true.

Could this be done in JS?



On 23 December 2010 15:21, T.J. Crowder t...@crowdersoftware.com wrote:
 @Richard:

 Maybe something like ...

 ['keyup', 'click', 'focus', 'blur'].each(function(eventName){
  $('filter').observe(eventName, function(evt){
   this.fire('check:filter');
  });

 });

 That creates four identical functions (one for each event). Not
 necessarily a problem, but...

 @Walter: How 'bout *really* not repeating yourself: ;-)

 // In your bag of tricks
 Element.addMethods({
    observeAll: function(element, eventNames, handler) {
        if (!(element = $(element))) return;
        eventNames = typeof eventNames === string ? $w(eventNames) :
 eventNames;
        eventNames.each(function(eventName) {
            Event.observe(element, eventName, handler);
        });
        return element;
    }
 });

 // Then in this specific case:
 $('filter').observeAll(['keyup', 'click', 'focus', 'blur'],
 function(evt) {
   this.fire('check:filter');
 });

 // Or:
 $('filter').observeAll('keyup click focus blur', function(evt) {
   this.fire('check:filter');
 });

 // Or even:
 var filter = $('filter');
 filter.observeAll(
   'keyup click focus blur',
   Element.fire.curry(filter, 'check:filter'));

 FWIW,
 --
 T.J. Crowder
 Independent Software Engineer
 tj / crowder software / com
 www / crowder software / com

 On Dec 23, 10:02 am, Richard Quadling rquadl...@gmail.com wrote:
 On 22 December 2010 18:16, Walter Lee Davis wa...@wdstudio.com wrote:









  I have a quick filter for hiding list items until only matches show. I want
  to cover all the various ways that a user might interact with the search
  field, so I write this lovely:

         $('filter').observe('keyup', function(evt){
                 this.fire('check:filter');
         });
         $('filter').observe('click', function(evt){
                 this.fire('check:filter');
         });
         $('filter').observe('focus', function(evt){
                 this.fire('check:filter');
         });
         $('filter').observe('blur', function(evt){
                 this.fire('check:filter');
         });

  Is there any way to write this more clearly, as in with fewer lines of 
  code?

  I'm using 1.6.latest, haven't tried the new 1.7 goodies yet. Would that
  help?

  Walter

  --
  You received this message because you are subscribed to the Google Groups
  Prototype  script.aculo.us group.
  To post to this group, send email to
  prototype-scriptacul...@googlegroups.com.
  To unsubscribe from this group, send email to
  prototype-scriptaculous+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/prototype-scriptaculous?hl=en.

 Maybe something like ...

 ['keyup', 'click', 'focus', 'blur'].each(function(eventName){
  $('filter').observe(eventName, function(evt){
   this.fire('check:filter');
  });

 });

 --
 Richard Quadling
 Twitter : EE : Zend
 @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

 --
 You received this message because you are subscribed to the Google Groups 
 Prototype  script.aculo.us group.
 To post to this group, send email to prototype-scriptacul...@googlegroups.com.
 To unsubscribe from this group, send email to 
 prototype-scriptaculous+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/prototype-scriptaculous?hl=en.





-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

-- 
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.