Re: [Catalyst] RE: DBIC - JSON conversion for AJAX

2011-10-07 Thread Alexander Hartmaier

Catalyst::Controller::DBIC::API was written for exactly that use case.

Cheers, Alex

Am 2011-09-12 16:38, schrieb Roland Philibert:

Thanks all for your suggestions so far.
I was I guess on the right track with JSON:XS but I had also seen REST
but so far I am still unsure as what the best way to go.
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/


***
T-Systems Austria GesmbH   Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
***
Notice: This e-mail contains information that is confidential and may be 
privileged.
If you are not the intended recipient, please notify the sender and then
delete this e-mail immediately.
***

___
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/


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

2011-09-14 Thread Roland Philibert
Eden or anybody familiar on this subject,

I tried your solution (REST + DBIx::..::HashRefInflator) but because my
search retrieve more than one rows, I populated an array as per:

sub thing_GET {

my ($self, $c) = @_;  
my @arr=();
my $rs = $c-model('DB::data)-search({}, {
result_class =
'DBIx::Class::ResultClass::HashRefInflator',
});

while (my $hashref = $rs-next) {
push @arr,$hashref;
}

$self-status_ok(   
$c,
entity = \@arr
);
}



Would you say that this is a valid approach? Or can I retrieve all data
bu other means with the DBIx model?

Thanks
Roland














-Original Message-
From: Roland Philibert 
Sent: 13 September 2011 16:56
To: The elegant MVC web framework
Subject: RE: [Catalyst] RE: DBIC - JSON conversion for AJAX

Eden Then if you ask for a Content-Type of application/json you'll get
JSON from that in your response.

I would do that by adding to the REST controller:
sub end :Private {
my ($self, $c) = @_; 
$c-forward(View::JSON);  
}
Is that correct?






-Original Message-
From: Eden Cardim [mailto:edencar...@gmail.com] 
Sent: 12 September 2011 21:31
To: The elegant MVC web framework
Subject: Re: [Catalyst] RE: DBIC - JSON conversion for AJAX

 Roland == Roland Philibert rphilib...@aptina.com writes:

Roland Thanks all for your suggestions so far.  I was I guess on
Roland the right track with JSON:XS but I had also seen REST but so
Roland far I am still unsure as what the best way to go

Catalyst::Controller::REST + DBIx::Class::ResultClass::HashRefInflator
works pretty well:

   $self-status_ok(
$c,
entity = $rs-search({}, {
  result_class =  'DBIx::Class::ResultClass::HashRefInflator'
})-next
   );

Then if you ask for a Content-Type of application/json you'll get JSON
from that in your response.

-- 
  Eden Cardim
  Code Monkeyhttp://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment
platform?
http://blog.edencardim.com/
http://www.shadowcat.co.uk/servers/
http://twitter.com/#!/edenc

___
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/
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/
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/


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

2011-09-14 Thread Ian.Docherty
search calls -all() implicitly on the resultset in list context so the 
following should work.

sub thing_GET {
my ($self, $c) = @_;  
my @arr = $c-model('DB::data)-search({}, {
result_class =
'DBIx::Class::ResultClass::HashRefInflator',
});

$self-status_ok(   
$c,
entity = \@arr
);
}

-Original Message-
From: Roland Philibert [mailto:rphilib...@aptina.com] 
Sent: 14 September 2011 15:54
To: The elegant MVC web framework
Subject: RE: [Catalyst] RE: DBIC - JSON conversion for AJAX

Eden or anybody familiar on this subject,

I tried your solution (REST + DBIx::..::HashRefInflator) but because my
search retrieve more than one rows, I populated an array as per:

sub thing_GET {

my ($self, $c) = @_;  
my @arr=();
my $rs = $c-model('DB::data)-search({}, {
result_class =
'DBIx::Class::ResultClass::HashRefInflator',
});

while (my $hashref = $rs-next) {
push @arr,$hashref;
}

$self-status_ok(   
$c,
entity = \@arr
);
}



Would you say that this is a valid approach? Or can I retrieve all data
bu other means with the DBIx model?

Thanks
Roland




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


___
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/


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

2011-09-14 Thread Roland Philibert
I did try what you suggest as per: 
sub thing_GET {

my ($self, $c) = @_;
$self-status_ok(   
$c,
entity = $rs =
$c-model('DB::data)-search({},{result_class =
'DBIx::Class::ResultClass::HashRefInflator',})-all
);
} 
...but the json rest object was showing just the number of row retrieved
ie: {rest:80}




-Original Message-
From: ian.doche...@nomura.com [mailto:ian.doche...@nomura.com] 
Sent: 14 September 2011 16:13
To: catalyst@lists.scsys.co.uk
Subject: RE: [Catalyst] RE: DBIC - JSON conversion for AJAX

search calls -all() implicitly on the resultset in list context so the
following should work.

sub thing_GET {
my ($self, $c) = @_;  
my @arr = $c-model('DB::data)-search({}, {
result_class =
'DBIx::Class::ResultClass::HashRefInflator',
});

$self-status_ok(   
$c,
entity = \@arr
);
}

-Original Message-
From: Roland Philibert [mailto:rphilib...@aptina.com] 
Sent: 14 September 2011 15:54
To: The elegant MVC web framework
Subject: RE: [Catalyst] RE: DBIC - JSON conversion for AJAX

Eden or anybody familiar on this subject,

I tried your solution (REST + DBIx::..::HashRefInflator) but because my
search retrieve more than one rows, I populated an array as per:

sub thing_GET {

my ($self, $c) = @_;  
my @arr=();
my $rs = $c-model('DB::data)-search({}, {
result_class =
'DBIx::Class::ResultClass::HashRefInflator',
});

while (my $hashref = $rs-next) {
push @arr,$hashref;
}

$self-status_ok(   
$c,
entity = \@arr
);
}



Would you say that this is a valid approach? Or can I retrieve all data
bu other means with the DBIx model?

Thanks
Roland




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


___
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/
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/


Re: [Catalyst] RE: DBIC - JSON conversion for AJAX

2011-09-14 Thread Ronald J Kimball
On Wed, Sep 14, 2011 at 11:44 AM, Roland Philibert rphilib...@aptina.comwrote:

 I did try what you suggest as per:
 sub thing_GET {

my ($self, $c) = @_;
 $self-status_ok(
$c,
entity = $rs =
 $c-model('DB::data)-search({},{result_class =
 'DBIx::Class::ResultClass::HashRefInflator',})-all
);
 }
 ...but the json rest object was showing just the number of row retrieved
 ie: {rest:80}


That is not quite what Ian suggested.  Scalar assignment (i.e. $rs =)
provides scalar context to the right-hand side, so you're calling all() in
scalar context, so you get the number of elements.  Also, the assignment to
$rs is misleading, as you're not actually assigning a ResultSet object to
it, but rather the result of calling -all() on a ResultSet.

Try one of these approaches instead, which all do the same thing in slightly
different ways.  Ian's original suggestion:

@arr = $c-model('DB::data)-search({},{result_class
= 'DBIx::Class::ResultClass::HashRefInflator',});
...
  entity = \@arr
...

or, with an anonymous array instead of a temporary array variable:

...
  entity = [$c-model('DB::data)-search({},{result_class
= 'DBIx::Class::ResultClass::HashRefInflator',})]
...

or, assigning the ResultSet to $rs first:

$rs = $c-model('DB::data)-search({},{result_class
= 'DBIx::Class::ResultClass::HashRefInflator',});
...
  entity = [$rs-all],
...

Ronald
___
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/


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

2011-09-14 Thread Ian.Docherty
That's because you didn't do exactly what I suggested. :)

You rolled up the creation of an array and the assignment into one, in doing so 
you called the search()-all in a scalar context, which will of course give you 
the number of rows.

If you really want to do that I think the following should work (not tested)

sub thing_GET {

my ($self, $c) = @_;
$self-status_ok(   
$c,
entity = [$c-model('DB::data)-search({},{result_class =
'DBIx::Class::ResultClass::HashRefInflator',})-all ],
);
}


-Original Message-
From: Roland Philibert [mailto:rphilib...@aptina.com] 
Sent: 14 September 2011 16:44
To: The elegant MVC web framework
Subject: RE: [Catalyst] RE: DBIC - JSON conversion for AJAX

I did try what you suggest as per: 
sub thing_GET {

my ($self, $c) = @_;
$self-status_ok(   
$c,
entity = $rs =
$c-model('DB::data)-search({},{result_class =
'DBIx::Class::ResultClass::HashRefInflator',})-all
);
} 
...but the json rest object was showing just the number of row retrieved
ie: {rest:80}




-Original Message-
From: ian.doche...@nomura.com [mailto:ian.doche...@nomura.com] 
Sent: 14 September 2011 16:13
To: catalyst@lists.scsys.co.uk
Subject: RE: [Catalyst] RE: DBIC - JSON conversion for AJAX

search calls -all() implicitly on the resultset in list context so the
following should work.

sub thing_GET {
my ($self, $c) = @_;  
my @arr = $c-model('DB::data)-search({}, {
result_class =
'DBIx::Class::ResultClass::HashRefInflator',
});

$self-status_ok(   
$c,
entity = \@arr
);
}




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


___
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/


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

2011-09-14 Thread Roland Philibert
Thanks for the tip Ronald,  works like nicely.


Cheers,

Roland

 

 

 

 

 

From: Ronald J Kimball [mailto:rkimb...@pangeamedia.com] 
Sent: 14 September 2011 16:59
To: The elegant MVC web framework
Subject: Re: [Catalyst] RE: DBIC - JSON conversion for AJAX

 

On Wed, Sep 14, 2011 at 11:44 AM, Roland Philibert
rphilib...@aptina.com wrote:

I did try what you suggest as per:

sub thing_GET {

   my ($self, $c) = @_;

   $self-status_ok(
   $c,
   entity = $rs =

$c-model('DB::data)-search({},{result_class =

'DBIx::Class::ResultClass::HashRefInflator',})-all
   );
}
...but the json rest object was showing just the number of row retrieved
ie: {rest:80}

 

 

That is not quite what Ian suggested.  Scalar assignment (i.e. $rs =)
provides scalar context to the right-hand side, so you're calling all()
in scalar context, so you get the number of elements.  Also, the
assignment to $rs is misleading, as you're not actually assigning a
ResultSet object to it, but rather the result of calling -all() on a
ResultSet.

 

Try one of these approaches instead, which all do the same thing in
slightly different ways.  Ian's original suggestion:

 

@arr = $c-model('DB::data)-search({},{result_class =
'DBIx::Class::ResultClass::HashRefInflator',});

...

  entity = \@arr

...

 

or, with an anonymous array instead of a temporary array variable:

 

...

  entity = [$c-model('DB::data)-search({},{result_class =
'DBIx::Class::ResultClass::HashRefInflator',})]

...

 

or, assigning the ResultSet to $rs first:

 

$rs = $c-model('DB::data)-search({},{result_class =
'DBIx::Class::ResultClass::HashRefInflator',});

...

  entity = [$rs-all],

...

 

Ronald

 


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/


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

2011-09-14 Thread Roland Philibert
I have tested it now, and this works very well.
Thanks for your help on this matter.





-Original Message-
From: ian.doche...@nomura.com [mailto:ian.doche...@nomura.com] 
Sent: 14 September 2011 17:22
To: catalyst@lists.scsys.co.uk
Subject: RE: [Catalyst] RE: DBIC - JSON conversion for AJAX

That's because you didn't do exactly what I suggested. :)

You rolled up the creation of an array and the assignment into one, in
doing so you called the search()-all in a scalar context, which will of
course give you the number of rows.

If you really want to do that I think the following should work (not
tested)

sub thing_GET {

my ($self, $c) = @_;
$self-status_ok(   
$c,
entity = [$c-model('DB::data)-search({},{result_class
=
'DBIx::Class::ResultClass::HashRefInflator',})-all ],
);
}


-Original Message-
From: Roland Philibert [mailto:rphilib...@aptina.com] 
Sent: 14 September 2011 16:44
To: The elegant MVC web framework
Subject: RE: [Catalyst] RE: DBIC - JSON conversion for AJAX

I did try what you suggest as per: 
sub thing_GET {

my ($self, $c) = @_;
$self-status_ok(   
$c,
entity = $rs =
$c-model('DB::data)-search({},{result_class =
'DBIx::Class::ResultClass::HashRefInflator',})-all
);
} 
...but the json rest object was showing just the number of row retrieved
ie: {rest:80}




-Original Message-
From: ian.doche...@nomura.com [mailto:ian.doche...@nomura.com] 
Sent: 14 September 2011 16:13
To: catalyst@lists.scsys.co.uk
Subject: RE: [Catalyst] RE: DBIC - JSON conversion for AJAX

search calls -all() implicitly on the resultset in list context so the
following should work.

sub thing_GET {
my ($self, $c) = @_;  
my @arr = $c-model('DB::data)-search({}, {
result_class =
'DBIx::Class::ResultClass::HashRefInflator',
});

$self-status_ok(   
$c,
entity = \@arr
);
}




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


___
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/
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/


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

2011-09-13 Thread Roland Philibert
Eden Then if you ask for a Content-Type of application/json you'll get
JSON from that in your response.

I would do that by adding to the REST controller:
sub end :Private {
my ($self, $c) = @_; 
$c-forward(View::JSON);  
}
Is that correct?






-Original Message-
From: Eden Cardim [mailto:edencar...@gmail.com] 
Sent: 12 September 2011 21:31
To: The elegant MVC web framework
Subject: Re: [Catalyst] RE: DBIC - JSON conversion for AJAX

 Roland == Roland Philibert rphilib...@aptina.com writes:

Roland Thanks all for your suggestions so far.  I was I guess on
Roland the right track with JSON:XS but I had also seen REST but so
Roland far I am still unsure as what the best way to go

Catalyst::Controller::REST + DBIx::Class::ResultClass::HashRefInflator
works pretty well:

   $self-status_ok(
$c,
entity = $rs-search({}, {
  result_class =  'DBIx::Class::ResultClass::HashRefInflator'
})-next
   );

Then if you ask for a Content-Type of application/json you'll get JSON
from that in your response.

-- 
  Eden Cardim
  Code Monkeyhttp://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment
platform?
http://blog.edencardim.com/
http://www.shadowcat.co.uk/servers/
http://twitter.com/#!/edenc

___
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/
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/


[Catalyst] RE: DBIC - JSON conversion for AJAX

2011-09-12 Thread Ian.Docherty
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:rphilib...@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) {
    
        $('pI 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('pno 
result sorry../p');

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

2011-09-12 Thread Roland Philibert
Thanks all for your suggestions so far.
I was I guess on the right track with JSON:XS but I had also seen REST
but so far I am still unsure as what the best way to go.
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/