From c278916ad48d32a3d519a21c3b804382098fe9ce Mon Sep 17 00:00:00 2001 From: nathanvda Date: Wed, 12 Oct 2011 22:59:12 +0200 Subject: [PATCH] Added new callback solution, allowing both insertion and removal callbacks. --- app/assets/javascripts/cocoon.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/cocoon.js b/app/assets/javascripts/cocoon.js index fbb2e49..232daf5 100644 --- a/app/assets/javascripts/cocoon.js +++ b/app/assets/javascripts/cocoon.js @@ -5,6 +5,10 @@ content.replace(reg_exp, with_str); } + function trigger_removal_callback(node) { + node.parent().parent().trigger('removal-callback'); + } + $('.add_fields').live('click', function(e) { e.preventDefault(); var assoc = $(this).data('association'), @@ -13,6 +17,7 @@ insertionPosition = $(this).data('association-insertion-position'), insertionNode = $(this).data('association-insertion-node'), insertionCallback = $(this).data('insertion-callback'), + removalCallback = $(this).data('removal-callback'), regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'), regexp_underscord = new RegExp('_new_' + assoc + '_', 'g'), new_id = new Date().getTime(), @@ -41,18 +46,18 @@ } else { insertionNode.before(contentNode); } - - if (insertionCallback){ - insertionCallback.call(contentNode); - } + + $(this).parent().trigger('insertion-callback'); }); $('.remove_fields.dynamic').live('click', function(e) { + trigger_removal_callback($(this)); e.preventDefault(); $(this).closest(".nested-fields").remove(); }); $('.remove_fields.existing').live('click', function(e) { + trigger_removal_callback($(this)); e.preventDefault(); $(this).prev("input[type=hidden]").val("1"); $(this).closest(".nested-fields").hide();