symfony/symfony — pull request #7713 by leevigraham (2013-04-18 14:44:17+00:00)
Title: Added additional data attributes for collections

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes

I've added 3 extra data-* attributes to collections in twig templates and renamed the prototype attribute for easier js manipulation.

New attributes:

* `data-collection`: This is a collection and all children are form types
* `data-collection-add`: Allow adding of new elements
* `data-collection-delete`: Allow deletion of elements (new and existing)

This should make javascript integration much smoother.

Example js:

```js
$('[data-collection]').each(function(){
var $collection = $(this);
var count = $collection.children().length;
var prototypeString = $collection.data('collectionPrototype');
var $deleteLink = $("Remove");
var $addLink = $("Add");

if($collection.data('collectionDelete')) {
$collection.children().append($deleteLink.clone());
$collection.on('click', '.delete', function(){
event.preventDefault();
$(this).parent().remove();
})
}

if($collection.data('collectionAdd')) {
$addLink.click(function(event){
event.preventDefault();
newPrototypeString = prototypeString.replace(/__name__/g, count);
$newPrototype = $(newPrototypeString);
$newPrototype.append($deleteLink.clone());
$newPrototype.insertBefore($addLink);
count++;
});
$collection.append($addLink);
}
})
```

Replaces #5145