* added option to specify position of insertion for the add partial

This commit is contained in:
Mihai Târnovan 2011-03-03 19:22:23 +01:00
parent dc1ad12845
commit 75c3a9bacd

View File

@ -3,6 +3,8 @@ $(document).ready(function() {
$('.add_fields').live('click', function() { $('.add_fields').live('click', function() {
var assoc = $(this).attr('data-association'); var assoc = $(this).attr('data-association');
var content = $(this).siblings('#' + assoc + '_fields_template').html(); 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 regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g');
var new_id = new Date().getTime(); var new_id = new Date().getTime();
var new_content = content.replace(regexp_braced, '[' + new_id + ']'); var new_content = content.replace(regexp_braced, '[' + new_id + ']');
@ -10,7 +12,19 @@ $(document).ready(function() {
regexp_braced = new RegExp('\\[new_' + assoc + 's\\]', 'g'); regexp_braced = new RegExp('\\[new_' + assoc + 's\\]', 'g');
new_content = content.replace(regexp_braced, '[' + new_id + ']'); 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; return false;
}); });
@ -26,3 +40,4 @@ $(document).ready(function() {
}); });
}); });