Thanks, John R.

I've updated my demo page for the latest jQuery
(http://dmlair.com/jquery/jqIfElse/). I've also thought about the
issue of providing callbacks. If the $if() function is modified into
something like the following, then you could add a .$if(function()
{...}) to any jquery chain and the function will be executed if the
chain isn't empty. Further chaining could be done as the function's
return value is used as the $if() return value.

Since I haven't tested this in *any* way, for convenience I'll add it
to my demo page, but as $if2().

Here's the modified code:

     $.fn.$if2 = function(b) {
        if ( b && b.constructor == Function && this.length) {
           return b(this);
        } else {
           b = (typeof b == 'undefined') ? this.length : b;
           return this.pushStack(b ? this : []);
        }
     };

The $else() could be modified similarly, thusly:

     $.fn.$else2 = function(fn) {
        var returnEmpty = (!this.stack.length || (this.ifStack[0] &&
this.ifStack[0].length));
        if ( !returnEmpty && fn && fn.constructor == Function) {
          return fn(this.stack[0]);
        } else {
          return this.get(returnEmpty ? [] : this.stack[0]);
        }
     };

Again, I haven't tested either of these revisions, but it should give
both capabilities -- jQuery chaining and function callbacks.

John Klinger

On 9/30/06, John Resig <[EMAIL PROTECTED]> wrote:
> Although, going back through his code - I really like what he did. He
> made it such that you no longer had to use anonymous function wrappers
> - which I really like. It'd be really cool to be able to do:
>
> $("#foo")
>     .find("div")
>         .click()
>             .filter(".someclass")
>                 .addClass("test")
>             .else()
>                 .addClass("someclass")
>             .end()
>         .end();
>
> It looks like it also needs to be ported to 1.0.1. The code is here,
> for reference:
> http://dmlair.com/jquery/jqIfElse/
>
> --John
>
> On 9/30/06, John Resig <[EMAIL PROTECTED]> wrote:
> > > Well done, Jörn!
> > >
> > > It makes me remember the work of John Klinger:
> > > http://jquery.com/discuss/2006-July/#7292
> >
> > It reminded me of that too. I kind of like the simplicity of this
> > solution. Doing a chainable .else() like what John K did is
> > fundamentally "really hard".
> >
> > --John
> >
>
>
> --
> John Resig
> http://ejohn.org/
> [EMAIL PROTECTED]
>
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
>

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to