Skip to content

Instantly share code, notes, and snippets.

@jberger
Created January 4, 2017 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/1c1e301f31232b899d9cae5fe50c4162 to your computer and use it in GitHub Desktop.
Save jberger/1c1e301f31232b899d9cae5fe50c4162 to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
use DBM::Deep;
helper db => sub { state $db = DBM::Deep->new('test.db') };
app->db->{items} ||= [];
get '/' => 'index';
post '/' => sub {
my $c = shift;
my $items = $c->every_param('item');
$c->db->{items} = $items;
$c->redirect_to('index');
};
app->start;
__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html>
<head></head>
<body>
% my $items = db->{items};
%= form_for '/' => (method => 'POST') => begin
% for my $item (@$items) {
%= input_tag item => $item
% }
<button id="button_add" onclick="return add_input()">Add Item</button>
%= submit_button 'Save'
% end
%= javascript begin
function add_input () {
var button = document.getElementById('button_add');
var newItem = document.createElement('input');
newItem.name = 'item';
button.parentElement.insertBefore(newItem, button);
return false;
}
% end
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment