Removed superflous code, last version on my
blog<http://webreflection.blogspot.com/>,
but I probably gonna create a plugin into jQuery site, if anybody is
interested about it.
Regards
On Mon, Oct 27, 2008 at 12:31 PM, Andrea Giammarchi <
[EMAIL PROTECTED]> wrote:
> the output ...
>
>
> <body>
> <div>1</div>
> <div>2</div>
> <div>3</div>
> <div>4</div>
> <div>5</div>
> <div>6</div>
> <div>7</div>
> <div>8</div>
> </body>
>
> that's it :D
>
>
> On Mon, Oct 27, 2008 at 12:30 PM, Andrea Giammarchi <
> [EMAIL PROTECTED]> wrote:
>
>> jQuery plugin ... Ariel, just try if you like, forget if you don't.
>>
>> I am sure it is possible to optimize the procedure, rught now I am focused
>> on its functionality.
>>
>> (function($){ // Andrea Giammarchi Ultra Chain Suggestion
>> var slice = Array.prototype.slice;
>> $.fn.extend({
>> If:function(fn){
>> var _filter = this.filter(fn);
>> _filter._filter = this.filter(function(){return
>> !~_filter.index(this)});
>> _filter._parent = this;
>> _filter._removed = _filter;
>> return _filter;
>> },
>> ElseIf:function(fn){
>> var filter = function(){return !~_filter.index(this)},
>> _filter = this,
>> self = this._parent.filter(filter);
>> _filter = self.filter(fn);
>> _filter._filter = self.filter(filter);
>> _filter._parent = this._parent;
>> _filter._removed = $(slice.call(this._removed,
>> 0).concat(slice.call(_filter, 0)));
>> return _filter;
>> },
>> Else:function(){
>> var _filter = this._removed;
>> return this._parent.filter(function(){return !~_filter.index(this)});
>> },
>> Do:function(fn){
>> this.each(fn);
>> return this;
>> }
>> })})(jQuery);
>>
>> // Example
>> $(function(){
>> $("div")
>> .If(function(){return $(this).text() == "3" || $(this).text() ==
>> "5"})
>> .text("match the 3 or 5 check")
>> .ElseIf(function(){return $(this).text() & 1})
>> .text("odd numbers")
>> .ElseIf(function(){return $(this).text() == 2})
>> .Do(function(){
>> $(this).text("text is equal 2");
>> })
>> .ElseIf(function(){return $(this).text() == 6})
>> .text("match the 6 condition")
>> .Else()
>> .text("this is 4 or 8");
>> ;
>> })
>>
>> Best Regards,
>> Andrea
>>
>>
>>
>> On Mon, Oct 27, 2008 at 11:09 AM, Andrea Giammarchi <
>> [EMAIL PROTECTED]> wrote:
>>
>>> Ok, test completed, this is the idea as working example:
>>>
>>> [snip]
>>> Array.prototype.If = function(callback){
>>> var _filter = this.filter(callback);
>>> _filter._filter = this.filter(function(value){return
>>> !~_filter.indexOf(value)});
>>> _filter._parent = this;
>>> _filter._removed = _filter;
>>> return _filter;
>>> };
>>> Array.prototype.ElseIf = function(callback){
>>> var _filter = this,
>>> self = this._parent.filter(function(value){return
>>> !~_filter.indexOf(value)});
>>> _filter = self.filter(callback);
>>> _filter._filter = self.filter(function(value){return
>>> !~_filter.indexOf(value)});
>>> _filter._parent = this._parent;
>>> _filter._removed = this._removed.concat(_filter);
>>> return _filter;
>>> };
>>> Array.prototype.Else = function(callback){
>>> var _filter = this._removed;
>>> return this._parent.filter(function(value){return
>>> !~_filter.indexOf(value)});
>>> };
>>> Array.prototype.Do = function(){
>>> alert("Value: " + this);
>>> return this;
>>> };
>>>
>>> ([1,2,3,4,5,6,7,8])
>>> .If(function(v){return v === 3 || v === 5})
>>> .Do() // 3, 5
>>> .ElseIf(function(v){return v & 1})
>>> .Do() // 1, 7
>>> .ElseIf(function(v){return v === 2})
>>> .Do() // 2
>>> .ElseIf(function(v){return v === 6})
>>> .Do() // 6
>>> .Else()
>>> .Do() // 4, 8
>>> ;
>>>
>>> That's it, a "chain nightmare" but at the end an expected result in ultra
>>> chain. Dunno about performances, it is more an experiment than something
>>> else, at least it works, so get as is :-)
>>>
>>> Kind Regards
>>>
>>>
>>> On Mon, Oct 27, 2008 at 10:32 AM, Andrea Giammarchi <
>>> [EMAIL PROTECTED]> wrote:
>>>
>>>> Sorry Ariel, there is a logical chain error in my example, I'll prepare
>>>> a better one with a couple of Array prototypes and you'll deciede if it is
>>>> interesting or not, is it fine?
>>>>
>>>> Regards
>>>>
>>>>
>>>> On Mon, Oct 27, 2008 at 9:21 AM, Andrea Giammarchi <
>>>> [EMAIL PROTECTED]> wrote:
>>>>
>>>>> Ariel, thanks for the link, i quickly read it and it seems other people
>>>>> thought about that idea before.
>>>>> Anyway, my vision is more semantic and less complicated, since with a
>>>>> callback inside the if you can define many expressions, do operations, and
>>>>> then return true or false.
>>>>>
>>>>> What I mean is that your if().hasClass is limited to one or more jQuery
>>>>> operation, while if(callbackInScope) has no limits at all and inside you
>>>>> can
>>>>> return $(this).hasClass("whatever").
>>>>>
>>>>> In any case and for performances improvement, for the if else I would
>>>>> create a jQuery istance instead of callback
>>>>>
>>>>> this.if = jQuery(this);
>>>>> this.if._condition = 1; // as first condition to evaluate (make
>>>>> if(this._condition) check simpler)
>>>>>
>>>>> so te call will be something like
>>>>> $(elm).on("click")
>>>>> .if
>>>>> .hasClass('foo')
>>>>> .then()..
>>>>>
>>>>> As I said before, this implementation suffer about And or Or
>>>>> possibilities
>>>>>
>>>>> $(elm).on("click")
>>>>> .if
>>>>> .hasClass('foo')
>>>>> .And
>>>>> .text()
>>>>> .then()..
>>>>>
>>>>> as last limitation, you cannot compare properties:
>>>>>
>>>>> if.text() == "something"
>>>>>
>>>>> for this reason I suppose that the callback inside the
>>>>> If(whateverCallback) that as implementation will be simply a filter inside
>>>>> the method if
>>>>> something like
>>>>>
>>>>> If:function(fn){ // set first list of elements
>>>>> this._succeded = jQuery(this).filter(fn);
>>>>> return this._succeded;
>>>>> },
>>>>> ElseIf:function(fn){ // exclude precedent elements
>>>>> var _succeded = this._succeded,
>>>>> result = jQuery(this).filter(fn).filter(function(i,
>>>>> elm){return ~_succeded.indexOf(elm)});
>>>>> this._succeded =
>>>>> this._succeded.concat(Array.prototype.slice.call(result, 0));
>>>>> return result;
>>>>> },
>>>>> Else:function(){ // return only last list of elements
>>>>> var _succeded = this._succeded;
>>>>> returh this.filter(function(i, elm){return
>>>>> ~_succeded.indexOf(elm)});
>>>>> }
>>>>>
>>>>> I did not test above code, it is only to demonstrate the ligic behind.
>>>>> It does not seem to be that complicated but for sure it is more intense
>>>>> for
>>>>> CPU and could be more problematic for mem leaks
>>>>>
>>>>> Regards
>>>>>
>>>>>
>>>>> On Sun, Oct 26, 2008 at 11:42 PM, Ariel Flesler <[EMAIL PROTECTED]>wrote:
>>>>>
>>>>>> Andrea
>>>>>>
>>>>>> The if/else thing seems to make the code larger I think. If you need
>>>>>> to pass a lambda to If, then another to else of elseIf...
>>>>>>
>>>>>> It's an interesting idea and it came up before. I'm not so sure it's
>>>>>> brings clarity to code, though.
>>>>>>
>>>>>> Maybe .if().hasClass('foo').then()... but uhmm... seems to
>>>>>> complicated.
>>>>>>
>>>>>> Here you have the old post I mentioned, maybe it gives you some cool
>>>>>> idea :)
>>>>>>
>>>>>> http://groups.google.com/group/jquery-en/browse_thread/thread/43a69fa41f6e7089/6cebfee318992fe6
>>>>>>
>>>>>> Brandon disagreed back then as well... that's a good thing I think :D
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" 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/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---