Skip to content

Move JavaScript to separate file #8205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 10, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions reference/forms/types/collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ things are even easier because the ``data-prototype`` attribute is rendered
automatically for you (with a slight difference - see note below) and all
you need is the JavaScript:


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be reverted.

.. configuration-block::

.. code-block:: html+twig
Expand All @@ -167,7 +168,8 @@ you need is the JavaScript:

{# store the prototype on the data-prototype attribute #}
<ul id="email-fields-list"
data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}">
data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}"
data-widget-tags="{{ '<li></li>'|e }}">
{% for emailField in form.emails %}
<li>
{{ form_errors(emailField) }}
Expand All @@ -176,35 +178,43 @@ you need is the JavaScript:
{% endfor %}
</ul>

<a href="#" id="add-another-email">Add another email</a>
<a href="#"
class="add-another-collection-widget"
data-list="#email-field-list>Add another email</a>

{# ... #}
{{ form_end(form) }}

<script type="text/javascript">
// keep track of how many email fields have been rendered
var emailCount = '{{ form.emails|length }}';

jQuery(document).ready(function() {
jQuery('#add-another-email').click(function(e) {
e.preventDefault();

var emailList = jQuery('#email-fields-list');

// grab the prototype template
var newWidget = emailList.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, emailCount);
emailCount++;

// create a new list element and add it to the list
var newLi = jQuery('<li></li>').html(newWidget);
newLi.appendTo(emailList);
});
})
</script>
<script src="add-collection-widget.js"></script>

.. code-block:: javascript

// add-collection-widget.js
jQuery(document).ready(function () {
jQuery('.add-another-collection-widget').click(function (e) {
e.preventDefault();
var list = jQuery(jQuery(this).attr('data-list'));
// Try to find the counter of the list
var counter = list.data('widget-counter');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var counter = list.data('widget-counter') | list.children().length;

// If the counter does not exist, use the length of the list
if (!counter) { counter = list.children().length; }

// grab the prototype template
var newWidget = list.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, counter);
// Increase the counter
counter++;
// And store it, the length cannot be used if deleting widgets is allowed
list.data(' widget-counter', counter);

// create a new list element and add it to the list
var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
newElem.appendTo(list);
});
});

.. tip::

Expand Down