From 762906da1ba23408b0dd16c5420b8cb27e53cc0a Mon Sep 17 00:00:00 2001 From: Aron Carroll Date: Mon, 30 Apr 2012 14:16:24 +0100 Subject: [PATCH] Refactor related delete button Removed the inline click handlers and moved the JavaScript into application.js. Also remove the item with JavaScript rather than reload the page. --- ckan/public/scripts/application.js | 41 ++++++++++++++++-------- ckan/templates/_util.html | 6 ++-- ckan/templates/package/related_list.html | 19 ----------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/ckan/public/scripts/application.js b/ckan/public/scripts/application.js index 0d3628ebf76..5614ada8f69 100644 --- a/ckan/public/scripts/application.js +++ b/ckan/public/scripts/application.js @@ -1145,8 +1145,22 @@ CKAN.Utils = function($, my) { $('
').html(msg).hide().prependTo(form).fadeIn(); } + function relatedRequest(action, method, data) { + return $.ajax({ + type: method, + dataType: 'json', + contentType: 'application/json', + url: CKAN.SITE_URL + '/api/3/action/related_' + action, + data: data ? JSON.stringify(data) : undefined, + error: function(err, txt, w) { + // This needs to be far more informative. + addAlert('Error: Unable to ' + action + ' related item'); + } + }); + } + // Center thumbnails vertically. - $('.related-items').each(function () { + var relatedItems = $('.related-items').each(function () { var item = $(this); function vertiallyAlign() { @@ -1164,6 +1178,15 @@ CKAN.Utils = function($, my) { item.find('.description').truncate(); }); + // Add a handler for the delete buttons. + relatedItems.on('click', '[data-action=delete]', function (event) { + var id = $(this).data('relatedId'); + relatedRequest('delete', 'POST', {id: id}).done(function () { + $('#related-item-' + id).remove(); + }); + event.preventDefault(); + }); + $(form).submit(function (event) { event.preventDefault(); @@ -1186,18 +1209,10 @@ CKAN.Utils = function($, my) { return; } - $.ajax({ - type: this.method, - url: CKAN.SITE_URL + '/api/3/action/related_create', - data: JSON.stringify(data), - success: function (data) { - window.location.reload(); - }, - error: function(err, txt, w) { - // This needs to be far more informative. - addAlert('Error: Unable to add related item'); - } - }); + relatedRequest('create', this.method, data).done(function () { + // TODO: Insert item dynamically. + window.location.reload(); + }); }); }; diff --git a/ckan/templates/_util.html b/ckan/templates/_util.html index 5aa7c077c7e..8c2991877fd 100644 --- a/ckan/templates/_util.html +++ b/ckan/templates/_util.html @@ -126,16 +126,16 @@ -
  • +