CatalystでAjaxに挑戦してみた

おぞくてダサくてヘボいですが、メモ。

MyApp.pmの記述

use Catalyst qw/ConfigLoader
                               Static::Simple
                               FormValidator
                               Authentication
                               Authentication::Credential::Password
                               Authentication::Store::DBIC
                               Authorization::Roles
                               Session::CGISession
                               Prototype
                               Email::Japanese/;

テンプレートの記述

[% c.prototype.define_javascript_functions %]
<input type="text" name="regist_user_ID"><img src="[% static_root %]/common/img/check.gif" alt="確認" width="36" height="21">
[% c.prototype.observe_field('regist_user_ID',
       {
               url             => '/get_account_info',
               with    => "'search_key=name&id_code='+value",
               update  => 'regist_user_name',
       }) %]
<span id="regist_user_name"></span>

コントローラの記述

sub get_account_info : Global {
       my ( $self, $c ) = @_;

       my @applicants  = MyApp::Model::CDBI::User_detail->search(
               'id_code' => $c->req->param('id_code'),
       );
       my $ret;
       foreach my $applicant (@applicants){
               $ret    = $applicant->get($c->req->param('search_key'));
               Encode::from_to($ret, "sjis", "utf8");  # 文字コード変換処理 ACCESS(Shift_JIS) -> Catalyst(UTF-8)
       }

       if($ret){
               $c->res->output($ret);
       }
       else{
               $c->res->output('該当がありません');
       }
}