* 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
1 changed files with 17 additions and 2 deletions

View File

@ -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;
});
});
});