the differences to the user and either cancel the request and keep the newly
fetched data as current, or re-submit the change using the fresh version
number.
+
+DELETE /list/$list_id/$item_id
+------------------------------
+
+Deletes a list item. No request body. Always succeeds, even if the list item
+doesn't exist server-side, in which case the `list_version` isn't changed.
+
+Returns JSON object with the following keys:
+
+ - `list_version`: the new version of the shopping list whose item was deleted.
+ - `lists_version`: the current version of the list of shopping lists
return \%r;
};
+# delete shopping list item
+del '/list/:list_id/:item_id' => sub {
+ my $list_id = route_parameters->get('list_id');
+ length($list_id) and $list_id =~ /^\d{1,18}$/
+ or return invalid_input('bad list ID');
+
+ my $item_id = route_parameters->get('item_id');
+ length($item_id) and $item_id =~ /^\d{1,18}$/
+ or return invalid_input('bad item ID');
+
+ my $dbh = get_database;
+
+ my %r = (
+ lists_version => get_lists_version($dbh),
+ );
+
+ my $list = $dbh->resultset('ShoppingList')->find($list_id)
+ or return exception 404, 'No such list';
+
+ my $item = $dbh->resultset('ShoppingListItem')
+ ->find( { shopping_list => $list->id, id => $item_id } );
+
+ if ($item) {
+ $item->delete;
+ $list->update({version => $list->version + 1});
+ }
+
+ $r{list_version} = $list->version;
+
+ return \%r;
+};
+
true;
var item = $('<li>').addClass('list-item-row').data({
'lsl-uri': data.uri,
'lsl-version': data.version,
+ 'lsl-description': data.description,
'lsl-done': data.done});
var cb = $('<input type="checkbox">');
if (data.done) cb.prop('checked', true);
$('#new-list-item input').val('');
}
+function delete_list_item(dlg, li) {
+ $.ajax(li.data('lsl-uri'),
+ { type: 'DELETE' })
+ .done((resp) => {
+ li.remove();
+ dlg.dialog('destroy');
+ selected_list.data('lsl-version', selected_list.data('lsl-version')+1);
+ got_list_version(resp.list_version);
+ got_lists_version(resp.lists_version);
+ });
+}
+function edit_list_item(li) {
+ var d = $('<div>')
+ .append(
+ $('<fieldset>')
+ .append(
+ $('<legend>').text('Item name'),
+ $('<input type="text" size="10">')
+ .val(li.data('lsl-description'))
+ )
+ );
+
+ d.dialog({
+ dialogClass: 'edit-item-dialog',
+ autoOpen: true,
+ modal: true,
+ title: 'Edit item',
+ width: 'max-content',
+ buttons: [
+ {
+ class: 'btn-delete',
+ icon: 'ui-icon-trash',
+ click: () => {
+ delete_list_item(d, li);
+ },
+ },
+ {
+ text: 'Cancel',
+ click: ()=>{ d.dialog('destroy'); },
+ },
+ {
+ icon: 'ui-icon-disk',
+ text: 'OK',
+ click: () => {
+ save_list_item(d, li);
+ },
+ },
+ ],
+ });
+}
+function handle_list_item_edit(ev) {
+ var li = $(ev.target).closest('li');
+ edit_list_item(li);
+ return false;
+}
function handle_new_list_item_submission(){
var description = $('#new-list-item input[type="text"]').val().trim();
if (description.length == 0) return;
edit_list();
return false;
});
+ $('#list-items').on('click', '>li .edit-trigger', handle_list_item_edit);
$('#new-list-item button').on('click', handle_new_list_item_submission);
$('#new-list-item input').on('keypress', ev => {
if (13 == ev.keyCode) {