symfony/symfony — pull request #5145 by leevigraham (2012-08-01 14:30:09+00:00)
Title: Add extra data attributes for collections

PR for discussion.

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)

Renamed:

* data-prototype -> data-collection-protoype: Namespace prototype in the collection.

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);
$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);
}
})
```