ryanb / nested_form

Rails plugin to conveniently handle multiple models in a single form.
MIT License
1.79k stars 505 forks source link

add link to the triggered object in nested:fieldAdded #298

Open sartip opened 10 years ago

sartip commented 10 years ago

In the jquery_nested_form.js file you trigger an event when you add an item (set of fields) via javascript. This event has just one parameter: the fields object.

But I think it would be nice to also have the link you just clicked. This way you could access any attribute on that link that can be useful via javascript.

The code to add this is really simple:

var field = this.insertFields(content, assoc, link);
// bubble up event upto document (through form)
field
  .trigger({ type: 'nested:fieldAdded', field: field, link: link })
  .trigger({ type: 'nested:fieldAdded:' + assoc, field: field, link: link });
return false;

I just added a parameter with the variable that we already have (link: link).

This way I could have 2 or more link_to_add with different IDs or data-attributes and link the added field with the link clicked to personalize added field via javascript magic.