asJQuery prop: causes JS crash?

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

asJQuery prop: causes JS crash?

horrido
I have some code to collect input values from a form. However, I've encountered some very odd (buggy?) behaviour...

 | dict |
 dict
:= Dictionary new.
 
(('#myForm1 *' asJQuery) filter: ':input')
     each
: [ :thisArg :index |
         
(thisArg asJQuery attr: 'name') = 'Remember me' ifTrue: [
             dict at
: 'Remember me' put: ('#RememberMe' asJQuery prop: 'checked')
             
] ifFalse: [
         
(thisArg asJQuery attr: 'name') = 'Gender' ifTrue: [
             dict at
: 'Male' put: ('#Male' asJQuery val).
             dict at
: 'Female' put: ('#Female' asJQuery val)
             
] ifFalse: [
         
"default"
             dict at
: (thisArg asJQuery attr: 'name') put: (thisArg asJQuery val)
             
]]
 
] currySelf.

#RememberMe is a checkbox. #Male and #Female are radio buttons.

asJQuery prop: 'checked' causes an early break out of the loop. I know this because if I use asJQuery val instead, everything works and the loop completes.

I surmise that JavaScript is crapping out on prop: 'checked'. The odd thing is, the following code works perfectly elsewhere:

collectValues
    inputs
do: [ :each |
       
(each asJQuery attr: 'name') = 'Remember me' ifTrue: [
            dictionary at
: 'Remember me' put: ('#RememberMe' asJQuery prop: 'checked')
           
] ifFalse: [
       
(each asJQuery attr: 'name') = 'Gender' ifTrue: [
            dictionary at
: 'Male' put: ('#Male' asJQuery prop: 'checked').
            dictionary at
: 'Female' put: ('#Female' asJQuery prop: 'checked')
           
] ifFalse: [
       
"default"
            dictionary at
: (each asJQuery attr: 'name') put: (each asJQuery val)
           
]]
   
].

Any thoughts?

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: asJQuery prop: causes JS crash?

horrido
I have an answer...

The behaviour depends on the value of prop: 'checked'. If the value is false, then the loop is broken. I don't know why, but sending the yourself message to dict solves the problem. For example,

dict at: 'Male' put: ('#Male' asJQuery prop: 'checked');yourself.

I tried to understand why, but the truth eludes me...


On Saturday, 6 June 2015 12:25:12 UTC-4, Richard Eng wrote:
I have some code to collect input values from a form. However, I've encountered some very odd (buggy?) behaviour...

 | dict |
 dict
:= Dictionary new.
 
(('#myForm1 *' asJQuery) filter: ':input')
     each
: [ :thisArg :index |
         
(thisArg asJQuery attr: 'name') = 'Remember me' ifTrue: [
             dict at
: 'Remember me' put: ('#RememberMe' asJQuery prop: 'checked')
             
] ifFalse: [
         
(thisArg asJQuery attr: 'name') = 'Gender' ifTrue: [
             dict at
: 'Male' put: ('#Male' asJQuery val).
             dict at
: 'Female' put: ('#Female' asJQuery val)
             
] ifFalse: [
         
"default"
             dict at
: (thisArg asJQuery attr: 'name') put: (thisArg asJQuery val)
             
]]
 
] currySelf.

#RememberMe is a checkbox. #Male and #Female are radio buttons.

asJQuery prop: 'checked' causes an early break out of the loop. I know this because if I use asJQuery val instead, everything works and the loop completes.

I surmise that JavaScript is crapping out on prop: 'checked'. The odd thing is, the following code works perfectly elsewhere:

collectValues
    inputs
do: [ :each |
       
(each asJQuery attr: 'name') = 'Remember me' ifTrue: [
            dictionary at
: 'Remember me' put: ('#RememberMe' asJQuery prop: 'checked')
           
] ifFalse: [
       
(each asJQuery attr: 'name') = 'Gender' ifTrue: [
            dictionary at
: 'Male' put: ('#Male' asJQuery prop: 'checked').
            dictionary at
: 'Female' put: ('#Female' asJQuery prop: 'checked')
           
] ifFalse: [
       
"default"
            dictionary at
: (each asJQuery attr: 'name') put: (each asJQuery val)
           
]]
   
].

Any thoughts?

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: asJQuery prop: causes JS crash?

Herby Vojčík


Richard Eng wrote:

> I have an answer...
>
> The behaviour depends on the value of prop: 'checked'. If the value is
> false, then the loop is broken. I don't know why, but sending the
> yourself message to dict solves the problem. For example,
>
> ||
> dict at:'Male'put:('#Male'asJQuery prop:'checked');yourself.
>
> I tried to understand why, but the truth eludes me...

Ha ha, that's nice. I had to think for a while, but it's pretty easy after all. JQuery `each` has a "feature" of breaking the loop when you return false. Smalltalk dictionary at:put: (and every other similar API) consistenly return the value that is added, so result of your at:put: is the thing you added. Blocks are JS functions that return their last expression; so you returned false (the thing you added) if that checkbox was unchecked, thus broke out of the loop.

>
>
> On Saturday, 6 June 2015 12:25:12 UTC-4, Richard Eng wrote:
>
>     I have some code to collect input values from a form. However,
>    
 I've encountered some very odd (buggy?) behaviour...

>
>     ||
>     |dict |
>     dict :=Dictionarynew.
>     (('#myForm1 *'asJQuery)filter:':input')
>     each:[:thisArg :index |
>     (thisArg asJQuery attr:'name')='Remember me'ifTrue:[
>     dict at:'Remember me'put:('#RememberMe'asJQuery prop:'checked')
>     ]ifFalse:[
>     (thisArg asJQuery attr:'name')='Gender'ifTrue:[
>     dict at:'Male'put:('#Male'asJQuery val).
>     dict at:'Female'put:('#Female'asJQuery val)
>     ]ifFalse:[
>     "default"
>     dict at:(thisArg asJQuery attr:'name')put:(thisArg asJQuery val)
>     ]]
>     ]currySelf.
>
>     #RememberMe is a checkbox. #Male and #Female are radio buttons.
>
>     asJQuery prop:'checked' causes an early break out of the loop. I
>     know this because if I use asJQuery val instead, everything works
>     and the loop completes.
>
>     I surmise that JavaScript is crapping out on prop:'checked'. The
>     odd thing is, the following code works perfectly else
where:

>
>     ||
>     collectValues
>     inputs do:[:each |
>     (each asJQuery attr:'name')='Remember me'ifTrue:[
>     dictionary at:'Remember me'put:('#RememberMe'asJQuery prop:'checked')
>     ]ifFalse:[
>     (each asJQuery attr:'name')='Gender'ifTrue:[
>     dictionary at:'Male'put:('#Male'asJQuery prop:'checked').
>     dictionary at:'Female'put:('#Female'asJQuery prop:'checked')
>     ]ifFalse:[
>     "default"
>     dictionary at:(each asJQuery attr:'name')put:(each asJQuery val)
>     ]]
>     ].
>
>     Any thoughts?
>
> --
> You received this message because you are subscribed to the Google
> Groups "amber-lang" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [hidden email]
> <mailto:[hidden email]>.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "amber-lang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.