Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-05-26 20:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (94 votes). Check out past polls.

    Notices?