Created
January 4, 2017 23:09
-
-
Save jberger/1c1e301f31232b899d9cae5fe50c4162 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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