Add relative node selection

This commit is contained in:
Gauthier Delacroix 2012-08-28 19:08:57 +02:00
parent 6ebb375a67
commit 3833d9b175
2 changed files with 19 additions and 1 deletions

View File

@ -173,6 +173,7 @@ It takes four parameters:
- association: the name of the association (plural) of which a new instance needs to be added (symbol or string).
- html_options: extra html-options (see `link_to`)
There are some special options, the first three allow to control the placement of the new link-data:
- `data-association-insertion-traversal` : the jquery traversal method to allow node selection relative to the link. `closest`, `next`, `children`, etc. Default: absolute selection
- `data-association-insertion-node` : the jquery selector of the node
- `data-association-insertion-method` : jquery method that inserts the new data. `before`, `after`, `append`, `prepend`, etc. Default: `before`
- `data-association-insertion-position` : old method specifying where to insert new data.
@ -281,6 +282,18 @@ The `association-insertion-node` will determine where to add it. You can choose
The `association-insertion-method` will determine where to add it in relation with the node. Any jQuery DOM Manipulation method can be set but we recommend sticking to any of the following: `before`, `after`, `append`, `prepend`. It is unknown at this time what others would do.
The `association-insertion-traversal` will allow node selection to be relative to the link.
For example:
````javascript
$(document).ready(function() {
$("#owner a.add_fields").
data("association-insertion-method", 'append').
data("association-insertion-traversal", 'closest').
data("association-insertion-node", '#parent_table');
});
````
### Partial

View File

@ -21,6 +21,7 @@
content = $this.data('template'),
insertionMethod = $this.data('association-insertion-method') || $this.data('association-insertion-position') || 'before';
insertionNode = $this.data('association-insertion-node'),
insertionTraversal = $this.data('association-insertion-traversal'),
regexp_braced = new RegExp('\\[new_' + assoc + '\\]', 'g'),
regexp_underscord = new RegExp('_new_' + assoc + '_', 'g'),
new_id = new Date().getTime(),
@ -37,7 +38,11 @@
new_content = new_content.replace(regexp_underscord, newcontent_underscord);
if (insertionNode){
insertionNode = insertionNode == "this" ? $this : $(insertionNode);
if (insertionTraversal){
insertionNode = $this[insertionTraversal](insertionNode)
} else {
insertionNode = insertionNode == "this" ? $this : $(insertionNode);
}
} else {
insertionNode = $this.parent();
}