[Catalyst] RE: DBIC <-> JSON conversion for AJAX

Ian.Docherty at nomura.com Ian.Docherty at nomura.com
Mon Sep 12 12:39:51 GMT 2011


Roland
I like to use Catalyst::Controller::REST which will do the decoding and encoding of JSON for you. The module has some good examples.

I also like to 'decouple' the DBIC object from my view, so that I only pass in the values, not the object.

my $object = $c->model('MyModel::Foo')->find($object_id);
my $response = {
    foo     = $object->foo,
    bar     = $object->bar,
    bam     = $object->bam,
};
$c->stash->{ajax_response} = $response;

It should also be possible to create a 'flatten' method that does this for you in a generic manner, you could then have.

$c->stash->{ajax_response} = $object->flatten;

I have done this with none-DBIC objects, I am sure there must be a way to do it with DBIC as well.

Regards
Ian


From: Roland Philibert [mailto:rphilibert at aptina.com] 

Hello all,

I am new to Catalyst, so I will try to make this query as smart as I possibly can.

I am trying to fetch data from a mysql database using a jQuery ajax method to populate a select list...and my question is:  what is the recommended method in Catalyst to serialize/encode a DBIC class object into a JSON object, that I can then parse easily in a view with javascript?
If anyone is able to help, an example would be highly appreciated.

Many thanks
Roland

Ps: I have had an attempt with JSON::XS, but I don’t think it is correct as I had to bless manually the conversion. 
Not sure here that my approach is correct... I am thinking that perhaps the conversion should be done within the model itself?

Here is the way I think how the mechanics work with Catalyst, but my conversion does not return anything.

1./ in a TT view, I use the Jquery .ajax function to connect to a method (list_ajax) under the iprequest controller  eg:
                  $("#convenient")
                                 .click(function(){
                                       $.ajax({
                                                       type: "GET"
                                                                ,url: "[% c.uri_for("/request/list_ajax") %]"
                                                                ...


2./ the method gets the data from my database using a DBIC model ...but I encode it into a JSON object and stash it to the contents. (At this point here I am experimenting!)
       sub list_ajax :Local {
                                my ($self, $c) = @_;
                                my $encoder = encode "UTF-8", JSON::XS->new->allow_blessed(1)->convert_blessed(1)->encode($c->model('DB::request')->all);
                                $c->stash(ajax_request => [$encoder]);
                                $c->forward('View::JSON');
                }
                

3./ Coming back to my point 1 above, I get the contents with my ajax function by adding (in blue):
                  $("#convenient")
                                 .click(function(){
                                       $.ajax({
                                                       type: "GET"
                                                                ,url: "[% c.uri_for("/iprequest/list_ajax") %]"
                                                                ,dataType: "json"
                                                                ,cache: false
                                                                ,success: function(json){
                                                                                                                if(json.ajax_request) {
                                                                                                                                $('<p>I got something to show..</p>').appendTo('.reuseable);
                                                                                                                                $.each(json.ajax_request, function(i,n) {
                                                                                                                                                var item = json.ajax_request[i];
                                                                                                                                                $('<p>'+ item +'</p>')
                                                                                                                                                                .appendTo('.reuseable');
                                                                                                                                });
                                
                                                                                                                }
                                                                                                                else {
                                                                                                                                $('.reuseable').html('<p>no result sorry..</p>');
                                                                                                                                $('.reuseable').show();
                                                                                                                }
                                                                }
                                                });
                                });
                ....


Aptina (UK) Limited, Century Court, Millennium Way, Bracknell, Berkshire, RG12 2XT. Registered in England No. 06570543.

This e-mail and any attachments contain confidential information and are solely for the review and use of the intended recipient. If you have received this e-mail in error, please notify the sender and destroy this e-mail and any copies.




This e-mail (including any attachments) is confidential, may contain
proprietary or privileged information and is intended for the named
recipient(s) only. Unintended recipients are prohibited from taking action
on the basis of information in this e-mail and must delete all copies.
Nomura will not accept responsibility or liability for the accuracy or
completeness of, or the presence of any virus or disabling code in, this
e-mail. If verification is sought please request a hard copy. Any reference
to the terms of executed transactions should be treated as preliminary only
and subject to formal written confirmation by Nomura. Nomura reserves the
right to monitor e-mail communications through its networks (in accordance
with applicable laws). No confidentiality or privilege is waived or lost by
Nomura by any mistransmission of this e-mail. Any reference to "Nomura" is
a reference to any entity in the Nomura Holdings, Inc. group. Please read
our Electronic Communications Legal Notice which forms part of this e-mail:
http://www.Nomura.com/email_disclaimer.htm




More information about the Catalyst mailing list