Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to return stash data in JSON format

Status
Not open for further replies.

jj0914

Programmer
Aug 4, 2006
33
CA
currently I need to return one of my stashed data in JSON format, I know I can use Catalyst::View::JSON, but I am not sure how to use this module in my app. Can anyone give me a hand: Here is the my code for stashing data:
Code:
package myApp::Controller::Admin::Category;

use strict;
use warnings;
use JSON::Syck;

use base qw( myApp::Base::Controller );

use Data::FormValidator::Constraints::HTTP qw( POST );
use Encode qw( _utf8_off );

sub auto : Private {
    my ( $self, $c ) = @_;

    if ( my $category = $c->req->snippets->[ 0 ] ) {
        $c->stash->{ category }
            = $c->model( 'Links::Category' )->find( $category );
    }

    $c->stash->{ categories } = $c->model( 'Links::Category' )->search(
        { parent => undef, lang => $c->language },
        {   page => $c->req->param( 'page' ) || 1,
            rows => 10,
            order_by => 'title'
        }
    );

    $self->prepare_form( $c );
    return 1;
}

sub prepare_form {
    my ( $self, $c ) = @_;

    my $form = $c->stash->{ form }
        = $c->model( 'Links::Category' )->validator_profile(
        {   required           => [ qw( method title ) ],
            filters            => [ qw( trim ) ],
            constraint_methods => { method => POST, },
        },
        );

    $c->form( $form );
}

sub root : Chained('/') PathPart('admin/category') CaptureArgs(1) {
    my ( $self, $c, $category ) = @_;

    if (    $category
        and $c->stash->{ category }
        = $c->model( 'Links::Category' )->find( $category ) )
    {
        $c->detach( 'update' );
    }
}

sub create : Chained('/') PathPart('admin/category/create') Args(0) {
    my ( $self, $c ) = @_;

    $c->req->param( method => $c->req->method );

    if ( $c->form->success ) {
        $c->form->valid( lang => $c->language );
        $c->model( 'Links::Category' )->create( $c->form );
        $c->stash->{ template } = 'admin/category/create.tt';
    }
    else {
        $c->stash->{ template } = 'admin/category/form.tt';
    }
}

sub view : Chained( '/' ) PathPart( 'admin/category/index' ) Args(0) {
    my ( $self, $c ) = @_;

    $c->stash->{ template } = 'admin/category/index.tt';

}

sub update : Chained('root') PathPart('update') Args(0) {
    my ( $self, $c ) = @_;

    my $category = $c->stash->{ category };

    if ( $c->req->param( 'delete' ) ) {
        $c->res->redirect(
            $c->uri_for( '/admin/category', $category->id, 'delete' ) );
        return;
    }

    if ( $c->req->param( 'cancel' ) ) {
        $c->res->redirect( $c->uri_for( '/admin/category' ) );
        return;
    }

    if ( $c->form->success ) {
        $category->delete;
        $c->form->valid( lang => $c->language );
        $c->model( 'Links::Category' )->create( $c->form );
        $c->stash->{ template } = 'admin/category/update/success.tt';
    }
    else {
        $c->req->params( { $category->get_columns, %{ $c->req->params }, } );
    }
}

sub delete : Chained('root') PathPart('delete') Args(0) {
    my ( $self, $c ) = @_;

    my $category = $c->stash->{ category };

    if ( lc $c->req->method eq 'post' ) {
        $category->result_source->schema->txn_begin;

        eval {

            NALD::Links::Exception::FileNotFound->throw(
                message => "File Not Found" )
                if not $category->category_id;
            $category->delete;
        };

        if ( $c->stash->{ error } = $@ ) {
            $category->result_source->schema->txn_rollback;
            $c->stash->{ template } = 'admin/category/delete/failure.tt';
        }
        else {
            $category->result_source->schema->txn_commit;
            $c->stash->{ template } = 'admin/category/delete/success.tt';
        }
    }
    else {
        $c->stash->{ template } = 'admin/category/delete.tt';
    }
}

1;
$c->stash->{categories} in sub auto is the data I want to return in JSON format. Also after I return the data, I wanna the path as url in my AJAX call, what path do i need to give out? Thanks
 
anyone can give me a hand on this? Thank you
 
I can honestly say I have no clue :) but I just want you to know your not being ignored.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top