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('D B::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.
_______________________________________________ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/