JavaScript::Writer + Template::Declare

作者:   發佈於:  

I spent last 24 hours figured out a nice way to play JavaScript::Writer with Template::Declare. It was super neat. Download the this zip and check out the source code. It depends on an future version of JavaScript::Writer module that had not been released to CPAN yet, so it won't work for you. But you can see the source code in lib/JT/, line 141: link_to( "Hello", href => "", onclick => ajax_replace( "#area", "data/hello.html" ) ); And line 109, for the definition of ajax_replace: sub ajax_replace { my ( $target, $uri ) = @_; return sub { jQuery("#loading")->fadeIn( sub { jQuery("#area")->load($uri)->show( sub { jQuery("#loading")->fadeOut(); } ); } ); js->append("return false;"); }; } It returns a Perl sub, and eventually converted to a Javascript function that work as the onclick handler. One nice feature is, although the A tag and onclick handlers are put together in Perl code, they are seperated in HTML code. All generated javascript are put to the end of body. A elements get an auto-generated ID for latter reference. And it's all kept in perl so programmer worries nothing. Now it works great with CGI::Minimal, no model code in this spike. I want to somehow make a better integration of such concept into Jifty, or Catalyst. That feels like an assignment to myself in the coming and YAPC::Asia

Labels: javascript, perl