Skip to content

Commit e885f6e

Browse files
committed
fixed deleting of nested elements
1 parent 10a77c9 commit e885f6e

1 file changed

Lines changed: 20 additions & 75 deletions

File tree

Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,24 @@
11
$(function() {
2-
3-
//this function will remove the add buttons if
4-
//the limit of additional sections specified has been reached.
5-
//It will also rename the labels accordingly to what the user specified.
6-
//for example: Applicant to Co-Applicant
7-
$.fn.update_labels_and_buttons = function(){
8-
var add_btn = null;
9-
10-
for(var i=0;i<this.size();i++)
11-
{
12-
if($(this[i]).attr('class') == 'remove_nested_fields')
13-
this[i] = $(this[i]).closest('.fields').siblings('a.add_nested_fields')[0];
14-
15-
var add_btn = $(this[i]);
16-
var siblings = $(this[i]).siblings('.fields');
17-
var children = add_btn.parents('.fields').children('.fields').children('a.add_nested_fields');
18-
19-
var limit = this[i].getAttribute('limit');
20-
var section_label = this[i].getAttribute('section');
21-
22-
if($(children).size() > 0)
23-
$(children).update_labels_and_buttons();
24-
25-
var remove_btn_array = $(siblings[0]).children('a.remove_nested_fields');
26-
27-
$(remove_btn_array).hide();
28-
29-
if(limit && siblings.size() >= limit)
30-
$(this[i]).hide();
31-
else
32-
$(this[i]).show();
33-
34-
for(var j=1;j<siblings.size();j++)
35-
{
36-
//update section
37-
if(section_label)
38-
{
39-
var label = (j > 1 ? (section_label + " " + j) : section_label);
40-
$(siblings[j]).children('.section').html(label);
41-
}
42-
43-
//remove duplicate fields if only the first occurence should be displayed
44-
$(siblings[j]).children('.input').each(function () {
45-
if($(this).attr('visible') == 'show_first_only')
46-
$(this).hide();
47-
});
48-
}
49-
}
50-
}
51-
52-
$(document).ready(function() {$('form a.add_nested_fields').update_labels_and_buttons()});
53-
54-
552
$('form a.add_nested_fields').live('click', function() {
3+
var $this = $(this);
564
// Setup
57-
var assoc = $(this).attr('data-association'); // Name of child
5+
var assoc = $this.attr('data-association'); // Name of child
586
var content = $('#' + assoc + '_fields_blueprint').html(); // Fields template
597

608
// Make the context correct by replacing new_<parents> with the generated ID
619
// of each of the parent objects
62-
var context = ($(this).closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
10+
var context = ($this.closest('.fields').find('input:first').attr('name') || '').replace(new RegExp('\[[a-z]+\]$'), '');
6311

6412
// context will be something like this for a brand new form:
6513
// project[tasks_attributes][1255929127459][assignments_attributes][1255929128105]
6614
// or for an edit form:
6715
// project[tasks_attributes][0][assignments_attributes][1]
68-
if(context) {
16+
if (context) {
6917
var parent_names = context.match(/[a-z_]+_attributes/g) || [];
7018
var parent_ids = context.match(/[0-9]+/g);
7119

72-
for(i = 0; i < parent_names.length; i++) {
73-
if(parent_ids[i]) {
20+
for (i = 0; i < parent_names.length; i++) {
21+
if (parent_ids[i]) {
7422
content = content.replace(
7523
new RegExp('(\\[' + parent_names[i] + '\\])\\[.+?\\]', 'g'),
7624
'$1[' + parent_ids[i] + ']'
@@ -84,31 +32,28 @@ $(function() {
8432
var new_id = new Date().getTime();
8533
content = content.replace(regexp, new_id);
8634

87-
var divToAdd = $(this).before(content);
88-
89-
$(divToAdd).prev().hide();
90-
$(divToAdd).prev().fadeIn(300, function(){
91-
$(divToAdd).prev().show();
92-
$(this).update_labels_and_buttons()
93-
});
94-
$(this).update_labels_and_buttons()
35+
$this.before(content);
36+
$this.closest(".fields").hide().fadeIn(500);
9537

9638
return false;
9739
});
9840

99-
$('form a.remove_nested_fields').live('click', function() {
100-
var hidden_field = $(this).prev('input[type=hidden]')[0];
41+
$("form a.remove_nested_fields").live("click", function() {
42+
var
43+
$this = $(this),
44+
$hidden_field = $this.prev("input[type=hidden]")[0],
45+
$fields = $this.closest(".fields");
10146

102-
if(hidden_field) {
103-
hidden_field.value = '1';
47+
if ($hidden_field) {
48+
$hidden_field.value = "1";
10449
}
10550

106-
var divToRemove = $(this).closest('.fields')
107-
divToRemove.fadeOut(300, function(){
108-
divToRemove.remove();
109-
$('a.add_nested_fields').update_labels_and_buttons();
51+
// delete all wrapped inputs
52+
// to properly handle required fields
53+
$fields.fadeOut(300, function(){
54+
$fields.children("div").remove();
11055
});
11156

11257
return false;
11358
});
114-
});
59+
});

0 commit comments

Comments
 (0)