[Dancer-users] generating client side code on the server
franck
franck at lumberjaph.net
Mon Jan 10 08:34:04 CET 2011
On Sat, Jan 8, 2011 at 1:43 AM, Puneet Kishor <punk.kish at gmail.com> wrote:
> Consider the most basic of workflows
>
> ## Perl
> get '/:foo_id.:format' => sub {
> my $foo_id = params->{foo_id};
>
> ## SQL
> my $sth = $dbh->prepare(qq{
> SELECT foo FROM table WHERE foo_id = ?
> });
> $sth->execute($foo_id);
> my ($foo) = $sth->fetchrow_array;
>
> to_json({data => $foo});
> };
>
>
> foo.tt
> ======
> <p>You got <span id="foo">[% foo %]</span></p>
> enter a number <input type="text" id="foo_id" name="foo_id">
> <input type="button" onclick="foo();">
>
> JavaScript
> ==========
> foo = function() {
> $.ajax({
> url : "/server/" + $("#foo_id").val() + ".json",
> type : "GET",
> data : "",
> dataType: "json",
> error : function() { alert("Error"); },
> success : function(data) {
> $("#foo").html(data.foo);
> }
> });
> }
>
>
> If I change the table column 'foo' to 'bar' and 'foo_id' to 'bar_id', I
> have to change my Perl code (which includes SQL), my template and
> JavaScript. Obviously, this can get untenable very quickly as the
> application becomes complex. The obvious solution is to generate the
> template and the JavaScript on the server side, thereby having to declare
> all the names in just one place. I have recently learned of the Perl module
> Jemplate that might enable me to do this. But before I commit any cycles to
> it, I want to know how the other Dancers handle something like this.
>
another solution is to use DBIx::Class, where you can declare your schema
with:
__PACKAGE__->add_columns( foo => {name => 'foo'}, ...);
and later change your schema to:
__PACKAGE__->add_columns(foo => {name => 'foo_id'}, ...);
so you don't have to change your code, only your schema.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20110110/5d52f2ca/attachment.htm>
More information about the Dancer-users
mailing list