I don't have my jqIfElse web page updated, I'll do that in the next couple of
days. However, here is updated code for the general $if/$else extension to
jQuery. Since it uses the jQuery internal stacks, it should work with
filter, is, add, not, etc. I have not tested it with anything but filter,
however.

One change to the original version is that if no parameters are passed to
if() it will traverse the "true" condition if the current jQuery handle has
objects.

     $.fn.pushStack = function(a,args) {
                var fn = args && args[args.length-1];

                if ( !fn || fn.constructor != Function ) {
                        var jq = this.get();
                        if ( !this.stack ) this.stack = [];
                        if ( !this.ifStack ) this.ifStack = [];    // Added line
                        this.stack.push( jq );
                        this.ifStack.push( a );   // Added line
                        this.get( a );
                } else {
                        var old = this.get();
                        this.get( a );
                        if ( fn.constructor == Function )
                                return this.each( fn );
                        this.get( old );
                }

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

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

     $.fn.end = function() {
        this.ifStack.pop();
        return this.get( this.stack.pop() );
     };

John Klinger



J?rn Zaefferer wrote:
> 
> I investigated on the idea a little further. A basic if/else, with 
> filter() as the if-method can be achieved with this change to filter():
> 
> filter: function(t) {
>         if( !this.filterStack ) this.filterStack = [];
>         this.filterStack.push(t);
>         return this.pushStack( ...);
> }
> 
> The else method, with a dollar sign prepended, looks like this:
> 
> $else: function() {
>     return this.end().not( this.filterStack.pop() );
> },
> 
> With those changes, this works:
>> $('#faq').find('dd').hide().end().find('dt').click(function() {
>>     $(this).next().filter(':visible')
>>         .slideUp()
>>     .$else()
>>         .slideDown();
>> });
>>   
> Hot or not?
> 
> -- J?rn
> 
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Chainable-if-else---hot-or-not--tf2363128.html#a6610843
Sent from the JQuery mailing list archive at Nabble.com.


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

Reply via email to