Javascript Condition with else decoration

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Javascript Condition with else decoration

Esteban A. Maringolo
I was trying to implement a classical "if {...} else {...}" object using JSDecorations.
Then I found there is a JSCondition, but there is no way to describe an "else" statement.

Also there is an JSIfThenElse decoration, but it is implemented as a ternary operator (x ? y : z) instead of 'if(x){ y} else {z}'

So I added an extension to JSObject, named #condition:else: and also a JSConditionElse decoration which produces the expected output.

Can this be added to the main trunk of the Javascript-Core package?

The changes are attached.


With these modifications I was able to transform this Javascript:
    $('#ajaxId tbody').on( 'click', 'tr', function () {
        if ( $(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        }
        else {
            $('#ajaxId tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
    } );

Into this:

| selClass |
selClass := 'selected'.
^ (html jQuery: '#' , self ajaxId , ' tbody')
on: 'click'
selector: 'tr'
do:
((html jQuery this removeClass: selClass)
condition: (html jQuery this hasClass: selClass)
else:
(JSScript new
add: ((html jQuery: '#' , self ajaxId , ' tr.' , selClass) removeClass: selClass);
add: (html jQuery this addClass: selClass);
yourself))


Esteban A. Maringolo

ps: An alternative solution would be to modify the JSIfThenElse, but this would require adding returns to the trueStatement and falseStatement of this class.

_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev

JSConditionElse.st (1K) Download Attachment
JSObject-conditionelse.st (496 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Javascript Condition with else decoration

Esteban A. Maringolo
No replies to this request in a week.
Can I have write access to the repo?

Who is the principal maintainer of Seaside?

Esteban A. Maringolo


2014-09-22 17:21 GMT-03:00 Esteban A. Maringolo <[hidden email]>:

> I was trying to implement a classical "if {...} else {...}" object using
> JSDecorations.
> Then I found there is a JSCondition, but there is no way to describe an
> "else" statement.
>
> Also there is an JSIfThenElse decoration, but it is implemented as a ternary
> operator (x ? y : z) instead of 'if(x){ y} else {z}'
>
> So I added an extension to JSObject, named #condition:else: and also a
> JSConditionElse decoration which produces the expected output.
>
> Can this be added to the main trunk of the Javascript-Core package?
>
> The changes are attached.
>
>
> With these modifications I was able to transform this Javascript:
>     $('#ajaxId tbody').on( 'click', 'tr', function () {
>         if ( $(this).hasClass('selected') ) {
>             $(this).removeClass('selected');
>         }
>         else {
>             $('#ajaxId tr.selected').removeClass('selected');
>             $(this).addClass('selected');
>         }
>     } );
>
> Into this:
>
> | selClass |
> selClass := 'selected'.
> ^ (html jQuery: '#' , self ajaxId , ' tbody')
> on: 'click'
> selector: 'tr'
> do:
> ((html jQuery this removeClass: selClass)
> condition: (html jQuery this hasClass: selClass)
> else:
> (JSScript new
> add: ((html jQuery: '#' , self ajaxId , ' tr.' , selClass) removeClass:
> selClass);
> add: (html jQuery this addClass: selClass);
> yourself))
>
>
> Esteban A. Maringolo
>
> ps: An alternative solution would be to modify the JSIfThenElse, but this
> would require adding returns to the trueStatement and falseStatement of this
> class.
_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
Reply | Threaded
Open this post in threaded view
|

Re: Javascript Condition with else decoration

Johan Brichau-2
Hi Esteban,

I picked up your request and was going to look at it but ran out of time.
The best idea is to create a ticket on https://code.google.com/p/seaside/issues/list

thanks
Johan

On 30 Sep 2014, at 15:24, Esteban A. Maringolo <[hidden email]> wrote:

No replies to this request in a week.
Can I have write access to the repo?

Who is the principal maintainer of Seaside?

Esteban A. Maringolo


2014-09-22 17:21 GMT-03:00 Esteban A. Maringolo <[hidden email]>:
I was trying to implement a classical "if {...} else {...}" object using
JSDecorations.
Then I found there is a JSCondition, but there is no way to describe an
"else" statement.

Also there is an JSIfThenElse decoration, but it is implemented as a ternary
operator (x ? y : z) instead of 'if(x){ y} else {z}'

So I added an extension to JSObject, named #condition:else: and also a
JSConditionElse decoration which produces the expected output.

Can this be added to the main trunk of the Javascript-Core package?

The changes are attached.


With these modifications I was able to transform this Javascript:
   $('#ajaxId tbody').on( 'click', 'tr', function () {
       if ( $(this).hasClass('selected') ) {
           $(this).removeClass('selected');
       }
       else {
           $('#ajaxId tr.selected').removeClass('selected');
           $(this).addClass('selected');
       }
   } );

Into this:

| selClass |
selClass := 'selected'.
^ (html jQuery: '#' , self ajaxId , ' tbody')
on: 'click'
selector: 'tr'
do:
((html jQuery this removeClass: selClass)
condition: (html jQuery this hasClass: selClass)
else:
(JSScript new
add: ((html jQuery: '#' , self ajaxId , ' tr.' , selClass) removeClass:
selClass);
add: (html jQuery this addClass: selClass);
yourself))


Esteban A. Maringolo

ps: An alternative solution would be to modify the JSIfThenElse, but this
would require adding returns to the trueStatement and falseStatement of this
class.
_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev


_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
Reply | Threaded
Open this post in threaded view
|

Re: Javascript Condition with else decoration

Esteban A. Maringolo
Oh... okay. My bad.

I submitted the issue #830 with the same contents of my initial email
https://code.google.com/p/seaside/issues/detail?id=830

Regards,

Esteban A. Maringolo


2014-09-30 11:48 GMT-03:00 Johan Brichau <[hidden email]>:

> Hi Esteban,
>
> I picked up your request and was going to look at it but ran out of time.
> The best idea is to create a ticket on
> https://code.google.com/p/seaside/issues/list
>
> thanks
> Johan
>
> On 30 Sep 2014, at 15:24, Esteban A. Maringolo <[hidden email]> wrote:
>
> No replies to this request in a week.
> Can I have write access to the repo?
>
> Who is the principal maintainer of Seaside?
>
> Esteban A. Maringolo
>
>
> 2014-09-22 17:21 GMT-03:00 Esteban A. Maringolo <[hidden email]>:
>
> I was trying to implement a classical "if {...} else {...}" object using
> JSDecorations.
> Then I found there is a JSCondition, but there is no way to describe an
> "else" statement.
>
> Also there is an JSIfThenElse decoration, but it is implemented as a ternary
> operator (x ? y : z) instead of 'if(x){ y} else {z}'
>
> So I added an extension to JSObject, named #condition:else: and also a
> JSConditionElse decoration which produces the expected output.
>
> Can this be added to the main trunk of the Javascript-Core package?
>
> The changes are attached.
>
>
> With these modifications I was able to transform this Javascript:
>    $('#ajaxId tbody').on( 'click', 'tr', function () {
>        if ( $(this).hasClass('selected') ) {
>            $(this).removeClass('selected');
>        }
>        else {
>            $('#ajaxId tr.selected').removeClass('selected');
>            $(this).addClass('selected');
>        }
>    } );
>
> Into this:
>
> | selClass |
> selClass := 'selected'.
> ^ (html jQuery: '#' , self ajaxId , ' tbody')
> on: 'click'
> selector: 'tr'
> do:
> ((html jQuery this removeClass: selClass)
> condition: (html jQuery this hasClass: selClass)
> else:
> (JSScript new
> add: ((html jQuery: '#' , self ajaxId , ' tr.' , selClass) removeClass:
> selClass);
> add: (html jQuery this addClass: selClass);
> yourself))
>
>
> Esteban A. Maringolo
>
> ps: An alternative solution would be to modify the JSIfThenElse, but this
> would require adding returns to the trueStatement and falseStatement of this
> class.
>
> _______________________________________________
> seaside-dev mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
>
>
>
> _______________________________________________
> seaside-dev mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
>
_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
Reply | Threaded
Open this post in threaded view
|

Re: Javascript Condition with else decoration

Johan Brichau-2
In reply to this post by Johan Brichau-2
Esteban,

As a short note, I forgot to reply that I was a tad confused by the naming of the method.
I would prefer to have something like #classicalthen: else: where the pattern stays <condition> <then> <else>, while your suggestion transforms it to <then> <condition> <else>

So, then we are down to choosing a name, which is probably why you ended up with #condition: else: 

Johan

On 30 Sep 2014, at 16:48, Johan Brichau <[hidden email]> wrote:

Hi Esteban,

I picked up your request and was going to look at it but ran out of time.
The best idea is to create a ticket on https://code.google.com/p/seaside/issues/list

thanks
Johan

On 30 Sep 2014, at 15:24, Esteban A. Maringolo <[hidden email]> wrote:

No replies to this request in a week.
Can I have write access to the repo?

Who is the principal maintainer of Seaside?

Esteban A. Maringolo


2014-09-22 17:21 GMT-03:00 Esteban A. Maringolo <[hidden email]>:
I was trying to implement a classical "if {...} else {...}" object using
JSDecorations.
Then I found there is a JSCondition, but there is no way to describe an
"else" statement.

Also there is an JSIfThenElse decoration, but it is implemented as a ternary
operator (x ? y : z) instead of 'if(x){ y} else {z}'

So I added an extension to JSObject, named #condition:else: and also a
JSConditionElse decoration which produces the expected output.

Can this be added to the main trunk of the Javascript-Core package?

The changes are attached.


With these modifications I was able to transform this Javascript:
   $('#ajaxId tbody').on( 'click', 'tr', function () {
       if ( $(this).hasClass('selected') ) {
           $(this).removeClass('selected');
       }
       else {
           $('#ajaxId tr.selected').removeClass('selected');
           $(this).addClass('selected');
       }
   } );

Into this:

| selClass |
selClass := 'selected'.
^ (html jQuery: '#' , self ajaxId , ' tbody')
on: 'click'
selector: 'tr'
do:
((html jQuery this removeClass: selClass)
condition: (html jQuery this hasClass: selClass)
else:
(JSScript new
add: ((html jQuery: '#' , self ajaxId , ' tr.' , selClass) removeClass:
selClass);
add: (html jQuery this addClass: selClass);
yourself))


Esteban A. Maringolo

ps: An alternative solution would be to modify the JSIfThenElse, but this
would require adding returns to the trueStatement and falseStatement of this
class.
_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev



_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
Reply | Threaded
Open this post in threaded view
|

Re: Javascript Condition with else decoration

Esteban A. Maringolo
IMHO JSIfThenElse should be the name of decoration class, and replace
all the references to it by JSTernary or similar.

But feel free to change it to whatever you like the most, I'm fine as
long as there is a way to define a JS if/then/else statement using
decorators.

Regards!


Esteban A. Maringolo


2014-09-30 12:14 GMT-03:00 Johan Brichau <[hidden email]>:

> Esteban,
>
> As a short note, I forgot to reply that I was a tad confused by the naming
> of the method.
> I would prefer to have something like #classicalthen: else: where the
> pattern stays <condition> <then> <else>, while your suggestion transforms it
> to <then> <condition> <else>
>
> So, then we are down to choosing a name, which is probably why you ended up
> with #condition: else:
>
> Johan
>
> On 30 Sep 2014, at 16:48, Johan Brichau <[hidden email]> wrote:
>
> Hi Esteban,
>
> I picked up your request and was going to look at it but ran out of time.
> The best idea is to create a ticket on
> https://code.google.com/p/seaside/issues/list
>
> thanks
> Johan
>
> On 30 Sep 2014, at 15:24, Esteban A. Maringolo <[hidden email]> wrote:
>
> No replies to this request in a week.
> Can I have write access to the repo?
>
> Who is the principal maintainer of Seaside?
>
> Esteban A. Maringolo
>
>
> 2014-09-22 17:21 GMT-03:00 Esteban A. Maringolo <[hidden email]>:
>
> I was trying to implement a classical "if {...} else {...}" object using
> JSDecorations.
> Then I found there is a JSCondition, but there is no way to describe an
> "else" statement.
>
> Also there is an JSIfThenElse decoration, but it is implemented as a ternary
> operator (x ? y : z) instead of 'if(x){ y} else {z}'
>
> So I added an extension to JSObject, named #condition:else: and also a
> JSConditionElse decoration which produces the expected output.
>
> Can this be added to the main trunk of the Javascript-Core package?
>
> The changes are attached.
>
>
> With these modifications I was able to transform this Javascript:
>    $('#ajaxId tbody').on( 'click', 'tr', function () {
>        if ( $(this).hasClass('selected') ) {
>            $(this).removeClass('selected');
>        }
>        else {
>            $('#ajaxId tr.selected').removeClass('selected');
>            $(this).addClass('selected');
>        }
>    } );
>
> Into this:
>
> | selClass |
> selClass := 'selected'.
> ^ (html jQuery: '#' , self ajaxId , ' tbody')
> on: 'click'
> selector: 'tr'
> do:
> ((html jQuery this removeClass: selClass)
> condition: (html jQuery this hasClass: selClass)
> else:
> (JSScript new
> add: ((html jQuery: '#' , self ajaxId , ' tr.' , selClass) removeClass:
> selClass);
> add: (html jQuery this addClass: selClass);
> yourself))
>
>
> Esteban A. Maringolo
>
> ps: An alternative solution would be to modify the JSIfThenElse, but this
> would require adding returns to the trueStatement and falseStatement of this
> class.
>
> _______________________________________________
> seaside-dev mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
>
>
>
>
> _______________________________________________
> seaside-dev mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
>
_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
Reply | Threaded
Open this post in threaded view
|

Re: Javascript Condition with else decoration

philippeback


Le 30 sept. 2014 17:19, "Esteban A. Maringolo" <[hidden email]> a écrit :
>
> IMHO JSIfThenElse should be the name of decoration class, and replace
> all the references to it by JSTernary or similar.
>
> But feel free to change it to whatever you like the most, I'm fine as
> long as there is a way to define a JS if/then/else statement using
> decorators.

Is there anyone having Cliff notes of the JS classes ?

It is actually quite hard to figure out how to make them all work together.

Phil
>
> Regards!
>
>
> Esteban A. Maringolo
>
>
> 2014-09-30 12:14 GMT-03:00 Johan Brichau <[hidden email]>:
> > Esteban,
> >
> > As a short note, I forgot to reply that I was a tad confused by the naming
> > of the method.
> > I would prefer to have something like #classicalthen: else: where the
> > pattern stays <condition> <then> <else>, while your suggestion transforms it
> > to <then> <condition> <else>
> >
> > So, then we are down to choosing a name, which is probably why you ended up
> > with #condition: else:
> >
> > Johan
> >
> > On 30 Sep 2014, at 16:48, Johan Brichau <[hidden email]> wrote:
> >
> > Hi Esteban,
> >
> > I picked up your request and was going to look at it but ran out of time.
> > The best idea is to create a ticket on
> > https://code.google.com/p/seaside/issues/list
> >
> > thanks
> > Johan
> >
> > On 30 Sep 2014, at 15:24, Esteban A. Maringolo <[hidden email]> wrote:
> >
> > No replies to this request in a week.
> > Can I have write access to the repo?
> >
> > Who is the principal maintainer of Seaside?
> >
> > Esteban A. Maringolo
> >
> >
> > 2014-09-22 17:21 GMT-03:00 Esteban A. Maringolo <[hidden email]>:
> >
> > I was trying to implement a classical "if {...} else {...}" object using
> > JSDecorations.
> > Then I found there is a JSCondition, but there is no way to describe an
> > "else" statement.
> >
> > Also there is an JSIfThenElse decoration, but it is implemented as a ternary
> > operator (x ? y : z) instead of 'if(x){ y} else {z}'
> >
> > So I added an extension to JSObject, named #condition:else: and also a
> > JSConditionElse decoration which produces the expected output.
> >
> > Can this be added to the main trunk of the Javascript-Core package?
> >
> > The changes are attached.
> >
> >
> > With these modifications I was able to transform this Javascript:
> >    $('#ajaxId tbody').on( 'click', 'tr', function () {
> >        if ( $(this).hasClass('selected') ) {
> >            $(this).removeClass('selected');
> >        }
> >        else {
> >            $('#ajaxId tr.selected').removeClass('selected');
> >            $(this).addClass('selected');
> >        }
> >    } );
> >
> > Into this:
> >
> > | selClass |
> > selClass := 'selected'.
> > ^ (html jQuery: '#' , self ajaxId , ' tbody')
> > on: 'click'
> > selector: 'tr'
> > do:
> > ((html jQuery this removeClass: selClass)
> > condition: (html jQuery this hasClass: selClass)
> > else:
> > (JSScript new
> > add: ((html jQuery: '#' , self ajaxId , ' tr.' , selClass) removeClass:
> > selClass);
> > add: (html jQuery this addClass: selClass);
> > yourself))
> >
> >
> > Esteban A. Maringolo
> >
> > ps: An alternative solution would be to modify the JSIfThenElse, but this
> > would require adding returns to the trueStatement and falseStatement of this
> > class.
> >
> > _______________________________________________
> > seaside-dev mailing list
> > [hidden email]
> > http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
> >
> >
> >
> >
> > _______________________________________________
> > seaside-dev mailing list
> > [hidden email]
> > http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
> >
> _______________________________________________
> seaside-dev mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev


_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
Reply | Threaded
Open this post in threaded view
|

Re: Javascript Condition with else decoration

Philippe Marschall
In reply to this post by Esteban A. Maringolo
On Mon, Sep 22, 2014 at 10:21 PM, Esteban A. Maringolo
<[hidden email]> wrote:

> I was trying to implement a classical "if {...} else {...}" object using
> JSDecorations.
> Then I found there is a JSCondition, but there is no way to describe an
> "else" statement.
>
> Also there is an JSIfThenElse decoration, but it is implemented as a ternary
> operator (x ? y : z) instead of 'if(x){ y} else {z}'
>
> So I added an extension to JSObject, named #condition:else: and also a
> JSConditionElse decoration which produces the expected output.
>
> Can this be added to the main trunk of the Javascript-Core package?
>
> The changes are attached.
>
>
> With these modifications I was able to transform this Javascript:
>     $('#ajaxId tbody').on( 'click', 'tr', function () {
>         if ( $(this).hasClass('selected') ) {
>             $(this).removeClass('selected');
>         }
>         else {
>             $('#ajaxId tr.selected').removeClass('selected');
>             $(this).addClass('selected');
>         }
>     } );
>
> Into this:
>
> | selClass |
> selClass := 'selected'.
> ^ (html jQuery: '#' , self ajaxId , ' tbody')
> on: 'click'
> selector: 'tr'
> do:
> ((html jQuery this removeClass: selClass)
> condition: (html jQuery this hasClass: selClass)
> else:
> (JSScript new
> add: ((html jQuery: '#' , self ajaxId , ' tr.' , selClass) removeClass:
> selClass);
> add: (html jQuery this addClass: selClass);
> yourself))
>
>
> Esteban A. Maringolo
>
> ps: An alternative solution would be to modify the JSIfThenElse, but this
> would require adding returns to the trueStatement and falseStatement of this
> class.

Because an if is not an expression in JavaScript?

Everything is a bit confusion at first but to me right now you change
looks like the simplest way to achieve what you want without
refactoring the whole hierarchies.

Cheers
Philippe
_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev
Reply | Threaded
Open this post in threaded view
|

Re: Javascript Condition with else decoration

Esteban A. Maringolo
2014-10-01 14:08 GMT-03:00 Philippe Marschall <[hidden email]>:
>> ps: An alternative solution would be to modify the JSIfThenElse, but this
>> would require adding returns to the trueStatement and falseStatement of this
>> class.
>
> Because an if is not an expression in JavaScript?

Ternary operator (? :), modeled with JSIfThenElse. can't have blocks
as parameters.

It is, you can have
(a > 3) ? 'Bigger' : 'Smaller';

but not:
(a > 3) ? {return 'Bigger' } : {return 'Smaller'}


> Everything is a bit confusion at first but to me right now you change
> looks like the simplest way to achieve what you want without
> refactoring the whole hierarchies.

Indeed. That's what I wanted. To be as non-invasive as possible.
The decoration gets a little awkward, but it does the job.

Regards,

Esteban A. Maringolo
_______________________________________________
seaside-dev mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/seaside-dev