Great ideas guys, how about this?

jQuery.fn.extend({
  cond: function() {
    var i, predicate, callback;
    for (i = 0; i < arguments.length; i += 2) {
      predicate = arguments[ i ];
      callback = arguments[ i + 1 ];
      if ( !callback ) {
        callback = predicate;
        predicate = true;
      }
      if ( jQuery.isFunction( predicate ) ? predicate.apply( this ) :
predicate ) {
        callback.apply( this );
        break;
      }
    }
    return this;
  }
});

function test() {
  return x === 3;
}

jQuery("a")
  .cond(
    x === 1, function(){
      this.css({ color: 'blue' });
    },

    x === 2, function(){
      this.css({ color: 'red' });
    },

    test, function(){
      this.css({ color: 'orange' });
    },

    function(){
      this.css({ color: 'green' });
    }
  );


On Jun 9, 2:46 pm, DBJDBJ <dbj...@gmail.com> wrote:
> After several iterations this discussion is nearing its (obvious?)
> conclusion.
> A LISP cond statement . For Lisp-ers COND is a thing of beauty. The
> basic syntax of COND is:
>
> (cond ((predicate1) (then do something 1))    ;if this predicate is
> true,
>                                               ;do something1
>
>       ((predicate2) (then do something 2))    ;if this predicate is
> true,
>                                               ;do something2,
>                                               ;each predicate and
> action
>                                               ;following the
>                                               ;first one is optional
>
>       (T (else do this))                      ;else, if none of the
>                                               ;predicates returns
>                                               ;TRUE, do this
> )
>
> So, please go ahead and implement jQuery.fn.cond()
> What's next? LAMBDA functions as an jQyery plugin ?
>
> Cheers ;o)
>
> --DBJ
>
> On Jun 9, 9:32 am, "stephb...@googlemail.com"
>
> <stephb...@googlemail.com> wrote:
> > This thread got me thinking, as chaining conditions is something I've
> > found myself wanting to do occasionally when quickly writing jQuery.
> > I'm not sure I'm sold on the idea of having the condition affect the
> > current chain, though.  I'd prefer something more like the event
> > helper syntax. Here's my contribution:
>
> > jQuery.fn.extend({
> >     condition: function() {
> >         for (var i=0; i<arguments.length; i=i+2) {
> >             if (arguments[i]) {
> >                 this.condition = (arguments[i+1]) ? arguments[i+1]:
> > arguments[i];
> >                 this.condition();
> >                 this.condition = jQuery.fn.condition;
> >                 break;
> >             }
> >         }
> >         return this;
> >     }
>
> > });
>
> > It allows you to write chainable jQuery conditions thus:
>
> > jQuery("#element").condition(condition, fn)
>
> > The arguments can contain any number of condition / function pairs
> > that act as 'if' and 'else if'.  If the final argument is a function
> > with no preceding condition
> > it acts as 'else'.  Inside the function 'this' is the current jQuery
> > collection:
>
> > jQuery("#element")
> >     .condition( (x===1), function(){
> >         this.css({color: 'blue'});
> >     }), (x===2), function(){
> >         this.css({color: 'red'});
> >     }), function(){
> >         this.css({color: 'green'});
> >     });
>
> > I'm really not sure of the merits of temporarily overwriting the
> > condition method to get 'this' to represent the current collection.
> > That's probably not clever. Certainly it means you can't put one
> > condition inside another. Does anyone know a better way, without
> > having to look for an unused name in the jQuery.fn.x namespace?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to