Pulled in the changes from erwin (pull #51)., with a few changes to clean up and maintain backwards compatibility.

This commit is contained in:
nathanvda 2012-04-07 01:33:30 +02:00
parent 7212a7a13c
commit 5047cdf0f1
2 changed files with 16 additions and 5 deletions

View File

@ -220,10 +220,15 @@ $(document).ready(function() {
$("#owner_from_list").show(); $("#owner_from_list").show();
$("#owner a.add_fields").show(); $("#owner a.add_fields").show();
}); });
$('#owner').bind("after-removal-callback",
function() {
/* e.g. recalculate order of child items */
});
}); });
```` ````
Do note that for the callbacks to work there has to be a surrounding container (div), where you can bind the callbacks to. Do note that for the callbacks to work there has to be a surrounding container (div), where you can bind the callbacks to.
Note that the default `removal-callback` is called _before_ removing the nested item.
### Control the Insertion behaviour ### Control the Insertion behaviour

View File

@ -6,7 +6,11 @@
} }
function trigger_removal_callback(node) { function trigger_removal_callback(node) {
node.parent().parent().trigger('removal-callback'); node.trigger('removal-callback');
}
function trigger_after_removal_callback(node) {
node.trigger('after-removal-callback');
} }
$('.add_fields').live('click', function(e) { $('.add_fields').live('click', function(e) {
@ -17,8 +21,6 @@
content = $this.data('template'), content = $this.data('template'),
insertionMethod = $this.data('association-insertion-method') || $this.data('association-insertion-position') || 'before'; insertionMethod = $this.data('association-insertion-method') || $this.data('association-insertion-position') || 'before';
insertionNode = $this.data('association-insertion-node'), insertionNode = $this.data('association-insertion-node'),
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(),
@ -52,17 +54,21 @@
$('.remove_fields.dynamic').live('click', function(e) { $('.remove_fields.dynamic').live('click', function(e) {
var $this = $(this); var $this = $(this);
trigger_removal_callback($this); var trigger_node = $this.closest(".nested-fields").parent();
trigger_removal_callback(trigger_node);
e.preventDefault(); e.preventDefault();
$this.closest(".nested-fields").remove(); $this.closest(".nested-fields").remove();
trigger_after_removal_callback(trigger_node);
}); });
$('.remove_fields.existing').live('click', function(e) { $('.remove_fields.existing').live('click', function(e) {
var $this = $(this); var $this = $(this);
trigger_removal_callback($this); var trigger_node = $this.closest(".nested-fields").parent().parent();
trigger_removal_callback(trigger_node);
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();
trigger_after_removal_callback(trigger_node);
}); });
})(jQuery); })(jQuery);