<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 12 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span lang=EN-GB>Hello all,<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB>I am new to Catalyst, so I will try to make this query as smart as I possibly can.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB>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?<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB>If anyone is able to help, an example would be highly appreciated.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB>Many thanks<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB>Roland<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB>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. <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB>Not sure here that my approach is correct... I am thinking that perhaps the conversion should be done within the model itself?<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB>Here is the way I think how the mechanics work with Catalyst, but my conversion does not return anything.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB>1./ in a TT view, I use the Jquery .ajax function to connect to a method (list_ajax) under the iprequest controller eg:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> $("#convenient")<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> .click(function(){<o:p></o:p></span></p><p class=MsoNormal style='text-indent:36.0pt'><span lang=EN-GB> $.ajax({<o:p></o:p></span></p><p class=MsoNormal style='text-indent:36.0pt'><span lang=EN-GB> type: "GET"<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> ,url: "[% c.uri_for("/<b><span style='background:yellow;mso-highlight:yellow'>request/list_ajax</span></b>") %]"<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#1F497D'> ...<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB>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!)<o:p></o:p></span></p><p class=MsoNormal style='text-indent:36.0pt'><span lang=EN-GB>sub <b><span style='background:yellow;mso-highlight:yellow'>list_ajax</span></b> :Local {<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> my ($self, $c) = @_;<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> my $encoder = encode "UTF-8", JSON::XS->new->allow_blessed(1)->convert_blessed(1)->encode($c->model('DB::request')->all);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> $c->stash(<b><span style='background:fuchsia;mso-highlight:fuchsia'>ajax_request</span></b> => [$encoder]);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> $c->forward('View::JSON');<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> }<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-GB>3./ Coming back to my point 1 above, I get the contents with my ajax function by adding (in blue):<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> $("#convenient")<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> .click(function(){<o:p></o:p></span></p><p class=MsoNormal style='text-indent:36.0pt'><span lang=EN-GB> $.ajax({<o:p></o:p></span></p><p class=MsoNormal style='text-indent:36.0pt'><span lang=EN-GB> type: "GET"<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> ,url: "[% c.uri_for("/iprequest/list_ajax") %]"<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> <span style='color:#365F91'>,dataType: "json"<o:p></o:p></span></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> ,cache: false<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> ,success: function(json){<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> if(json.<b><span style='background:fuchsia;mso-highlight:fuchsia'>ajax</span></b></span><b><span lang=EN-GB style='color:#1F497D;background:fuchsia;mso-highlight:fuchsia'>_</span></b><b><span lang=EN-GB style='color:#365F91;background:fuchsia;mso-highlight:fuchsia'>request</span></b><span lang=EN-GB style='color:#365F91'>) {<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> $('<p>I got something to show..</p>').appendTo('.reuseable);<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> $.each(json.ajax_request, function(i,n) {<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> var item = json.ajax_request[i];<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> $('<p>'+ item +'</p>')<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> .appendTo('.reuseable');<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> });<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> }<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> else {<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> $('.reuseable').html('<p>no result sorry..</p>');<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> $('.reuseable').show();<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> }<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> }<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> });<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='color:#365F91'> });<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB> ....<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB><o:p> </o:p></span></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>
<pre>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.