Right off the bat, I am positive this is a jQuery/Javascript issue, but before I ask questions on a forum that isn't Perlmonks, I'm hoping a Perl web monk could help.

I've got a Dancer2 application that works well. I have a couple code snips without any context, but know for fact that the following returns to the jQuery call either 0 or 1:

get '/get_aux/:aux' => sub { my $aux = params->{aux}; return _aux_state($aux); }; sub _aux_state { # maintains the auxillary state (on/off) my ($aux, $state) = @_; return $auxs->{$aux}{state} if ! defined $state; $auxs->{$aux}{state} = $state; return $state; }

Now, in my javascript, I *know* that I receive the proper information. On page load finish, I set up a button (which is a glorified checkbox). It's 'checked' value should be a call back to the Perl code. I've augmented and hacked the jquery code so I can see what's happening. In this code, I call aux_state() to set things up. LINE 1, depicted in comments, alerts undefined. However, while I'm in the aux_state() function itself making the call back to Perl (LINE 2), I get an alert with the expected result:

$(function(){ var state = aux_state('aux1'); alert(state); // LINE 1 $('#aux1').switchbutton({ checked: aux_state('aux1'), onChange: function(checked){ $.get(host +'/set_aux/aux1/'+ checked, function(state) +{ // ... }); } }); }); function aux_state(aux){ $.get(host +'/get_aux/' + aux, function(state){ alert(state); // LINE 2 return state; }); }

I'm totally missing something with jQuery me thinks. Can anyone point me in the right direction? This is my first real attempt at writing async jQuery code. If not, I will proceed to ask on SO.

I'm assuming my newbness in jquery is the issue here particularly with callbacks and returning things. If so, a pointer to a *good* doc would be handy. Even though I'm certain this isn't a Perl issue, I'd rather look at home before I go elsewhere (yep, I've searched online, but couldn't find anything conclusive perhaps because I don't know what to search for).

Thanks,

-stevieb

update: I'm currently reading this.


In reply to jQuery issue with Dancer2 by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.