-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+twig | ||
|
@@ -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) }} | ||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// 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:: | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be reverted.