Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Addition of a 'new_id' to the callback object #293

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Speedway Special
There are 2 differences between this and the main branch:

1. A 'new_id' addition to the callback engine for adding fields so that way you can put them into tabs
2. Modification to support inserting into a table when the last row of the table contains the link_to_add function

# Nested Form

[<img src="https://secure.travis-ci.org/ryanb/nested_form.png?branch=master" alt="Build Status" />](http://travis-ci.org/ryanb/nested_form)
Expand Down
13 changes: 9 additions & 4 deletions vendor/assets/javascripts/jquery_nested_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

// Make the context correct by replacing <parents> with the generated ID
// of each of the parent objects
var context = ($(link).closest('.fields').closestChild('input, textarea, select').eq(0).attr('name') || '').replace(/\[[a-z_]+\]$/, '');

if($(link).data('tabled')){
var context = ($(link).closest('table').closestChild('input, textarea, select').eq(0).attr('name') || '').replace(/\[[a-z_]+\]$/, '');
} else {
var context = ($(link).closest('.fields').closestChild('input, textarea, select').eq(0).attr('name') || '').replace(/\[[a-z_]+\]$/, '');
}
// If the parent has no inputs we need to strip off the last pair
var current = content.match(new RegExp('\\[([a-z_]+)\\]\\[new_' + assoc + '\\]'));
if (current) {
Expand Down Expand Up @@ -51,8 +54,8 @@
var field = this.insertFields(content, assoc, link);
// bubble up event upto document (through form)
field
.trigger({ type: 'nested:fieldAdded', field: field })
.trigger({ type: 'nested:fieldAdded:' + assoc, field: field });
.trigger({ type: 'nested:fieldAdded', field: field, new_id: new_id })
.trigger({ type: 'nested:fieldAdded:' + assoc, field: field, new_id: new_id });
return false;
},
newId: function() {
Expand All @@ -62,6 +65,8 @@
var target = $(link).data('target');
if (target) {
return $(content).appendTo($(target));
} else if ($(link).data('tabled')) {
return $(content).insertBefore($(link).closest('tr'))
} else {
return $(content).insertBefore(link);
}
Expand Down
14 changes: 10 additions & 4 deletions vendor/assets/javascripts/prototype_nested_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ document.observe('click', function(e, el) {

// Make the context correct by replacing <parents> with the generated ID
// of each of the parent objects
var context = (el.getOffsetParent('.fields').firstDescendant().readAttribute('name') || '').replace(/\[[a-z_]+\]$/, '');

var context;
if (el.readAttribute('data-tabled')) {
context = (el.getOffsetParent('table').firstDescendant().readAttribute('name') || '').replace(/\[[a-z_]+\]$/, '');
} else {
context = (el.getOffsetParent('.fields').firstDescendant().readAttribute('name') || '').replace(/\[[a-z_]+\]$/, '');
}
// If the parent has no inputs we need to strip off the last pair
var current = content.match(new RegExp('\\[([a-z_]+)\\]\\[new_' + assoc + '\\]'));
if (current) {
Expand Down Expand Up @@ -45,11 +49,13 @@ document.observe('click', function(e, el) {
var field;
if (target) {
field = $$(target)[0].insert(content);
} else if (el.readAttribute('data-tabled')) {
field = el.getOffsetParent('tr').insert({ before: content })
} else {
field = el.insert({ before: content });
}
field.fire('nested:fieldAdded', {field: field});
field.fire('nested:fieldAdded:' + assoc, {field: field});
field.fire('nested:fieldAdded', {field: field, new_id: new_id});
field.fire('nested:fieldAdded:' + assoc, {field: field, new_id: new_id});
return false;
}
});
Expand Down