[Dancer-users] generating client side code on the server
Puneet Kishor
punk.kish at gmail.com
Sat Jan 8 01:43:11 CET 2011
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.
Puneet.
--
Puneet Kishor http://punkish.org
Carbon Model http://carbonmodel.org
Charter Member, Open Source Geospatial Foundation http://www.osgeo.org
Science Fellow http://creativecommons.org/about/people/fellows#puneetkishor
Nelson Institute, UW-Madison http://www.nelson.wisc.edu
---------------------------------------------------------------------------
Assertions are politics; backing up assertions with evidence is science
===========================================================================
More information about the Dancer-users
mailing list