Added new callback solution, allowing both insertion and removal callbacks.

This commit is contained in:
nathanvda 2011-10-12 22:59:12 +02:00
parent a5c8f4e9a7
commit c278916ad4

View File

@ -5,6 +5,10 @@
content.replace(reg_exp, with_str); content.replace(reg_exp, with_str);
} }
function trigger_removal_callback(node) {
node.parent().parent().trigger('removal-callback');
}
$('.add_fields').live('click', function(e) { $('.add_fields').live('click', function(e) {
e.preventDefault(); e.preventDefault();
var assoc = $(this).data('association'), var assoc = $(this).data('association'),
@ -13,6 +17,7 @@
insertionPosition = $(this).data('association-insertion-position'), insertionPosition = $(this).data('association-insertion-position'),
insertionNode = $(this).data('association-insertion-node'), insertionNode = $(this).data('association-insertion-node'),
insertionCallback = $(this).data('insertion-callback'), insertionCallback = $(this).data('insertion-callback'),
removalCallback = $(this).data('removal-callback'),
regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'), regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'),
regexp_underscord = new RegExp('_new_' + assoc + '_', 'g'), regexp_underscord = new RegExp('_new_' + assoc + '_', 'g'),
new_id = new Date().getTime(), new_id = new Date().getTime(),
@ -41,18 +46,18 @@
} else { } else {
insertionNode.before(contentNode); insertionNode.before(contentNode);
} }
if (insertionCallback){ $(this).parent().trigger('insertion-callback');
insertionCallback.call(contentNode);
}
}); });
$('.remove_fields.dynamic').live('click', function(e) { $('.remove_fields.dynamic').live('click', function(e) {
trigger_removal_callback($(this));
e.preventDefault(); e.preventDefault();
$(this).closest(".nested-fields").remove(); $(this).closest(".nested-fields").remove();
}); });
$('.remove_fields.existing').live('click', function(e) { $('.remove_fields.existing').live('click', function(e) {
trigger_removal_callback($(this));
e.preventDefault(); e.preventDefault();
$(this).prev("input[type=hidden]").val("1"); $(this).prev("input[type=hidden]").val("1");
$(this).closest(".nested-fields").hide(); $(this).closest(".nested-fields").hide();