diff --git a/lib/generators/cocoon/install/templates/cocoon.js b/lib/generators/cocoon/install/templates/cocoon.js index 0bec43b..e0b6d25 100644 --- a/lib/generators/cocoon/install/templates/cocoon.js +++ b/lib/generators/cocoon/install/templates/cocoon.js @@ -3,6 +3,8 @@ $(document).ready(function() { $('.add_fields').live('click', function() { var assoc = $(this).attr('data-association'); var content = $(this).siblings('#' + assoc + '_fields_template').html(); + var insertionPosition = $(this).attr('data-association-insertion-position'); + var insertionNode = $(this).attr('data-association-insertion-node'); var regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'); var new_id = new Date().getTime(); var new_content = content.replace(regexp_braced, '[' + new_id + ']'); @@ -10,7 +12,19 @@ $(document).ready(function() { regexp_braced = new RegExp('\\[new_' + assoc + 's\\]', 'g'); new_content = content.replace(regexp_braced, '[' + new_id + ']'); } - $(this).parent().before(new_content); + if (insertionNode) { + insertionNode = $(insertionNode); + } + else { + insertionNode = $(this).parent(); + } + + if (insertionPosition == 'after'){ + insertionNode.after(new_content); + } else { + insertionNode.before(new_content); + } + return false; }); @@ -25,4 +39,5 @@ $(document).ready(function() { return false; }); -}); \ No newline at end of file +}); +