Last active
August 29, 2015 14:13
-
-
Save broncha/a8b597ce423242b38a5b to your computer and use it in GitHub Desktop.
Symfony prototype rendering macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% macro widget_prototype(widget, remove_text) %} | |
{% if widget.vars.prototype %} | |
{% set form = widget.vars.prototype %} | |
{% set name = widget.vars.name %} | |
{% else %} | |
{% set form = widget %} | |
{% set name = widget.vars.full_name %} | |
{% endif %} | |
<div data-content="{{ name }}"> | |
<a class="btn-remove" data-related="{{ name }}">{{ remove_text }}</a> | |
{{ form_widget(form) }} | |
</div> | |
{% endmacro %} | |
<div id="post_tags" data-prototype="{{ _self.widget_prototype(form.addresses, 'Remove address')|escape }}"> | |
{% for widget in form.addresses.children %} | |
{{ _self.widget_prototype(widget, 'Remove address') }} | |
{% endfor %} | |
</div> | |
<a class="btn-add" data-target="post_tags">Add address</a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
$('.btn-add').click(function(event) { | |
var collectionHolder = $('#' + $(this).attr('data-target')); | |
var prototype = collectionHolder.attr('data-prototype'); | |
var form = prototype.replace(/__name__/g, collectionHolder.children().length); | |
collectionHolder.append(form); | |
return false; | |
}); | |
$(document).on('click', ".btn-remove", function(event) { | |
var name = $(this).attr('data-related'); | |
$(this).parent('*[data-content="'+name+'"]').remove(); | |
return false; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment