@@ -290,19 +290,9 @@ functionality with JavaScript:
290
290
291
291
.. code-block :: javascript
292
292
293
- jQuery (document ).ready (function () {
294
- // Get the ul that holds the collection of tags
295
- var $tagsCollectionHolder = $ (' ul.tags' );
296
- // count the current form inputs we have (e.g. 2), use that as the new
297
- // index when inserting a new item (e.g. 2)
298
- $tagsCollectionHolder .data (' index' , $tagsCollectionHolder .find (' input' ).length );
299
-
300
- $ (' body' ).on (' click' , ' .add_item_link' , function (e ) {
301
- var $collectionHolderClass = $ (e .currentTarget ).data (' collectionHolderClass' );
302
- // add a new tag form (see next code block)
303
- addFormToCollection ($collectionHolderClass);
304
- })
305
- });
293
+ document
294
+ .querySelectorAll (' .add_item_link' )
295
+ .forEach (btn => btn .addEventListener (" click" , addFormToCollection));
306
296
307
297
The ``addFormToCollection() `` function's job will be to use the ``data-prototype ``
308
298
attribute to dynamically add a new form when this link is clicked. The ``data-prototype ``
@@ -312,42 +302,10 @@ you'll replace with a unique, incrementing number (e.g. ``task[tags][3][name]``)
312
302
313
303
.. code-block :: javascript
314
304
315
- function addFormToCollection ($collectionHolderClass ) {
316
- // Get the ul that holds the collection of tags
317
- var $collectionHolder = $ (' .' + $collectionHolderClass);
318
-
319
- // Get the data-prototype explained earlier
320
- var prototype = $collectionHolder .data (' prototype' );
321
-
322
- // get the new index
323
- var index = $collectionHolder .data (' index' );
324
-
325
- var newForm = prototype;
326
- // You need this only if you didn't set 'label' => false in your tags field in TaskType
327
- // Replace '__name__label__' in the prototype's HTML to
328
- // instead be a number based on how many items we have
329
- // newForm = newForm.replace(/__name__label__/g, index);
305
+ const addFormToCollection = (e ) => {
306
+ const collectionHolder = document .querySelector (' .' + e .currentTarget .dataset .collectionHolderClass );
330
307
331
- // Replace '__name__' in the prototype's HTML to
332
- // instead be a number based on how many items we have
333
- newForm = newForm .replace (/ __name__/ g , index);
334
-
335
- // increase the index with one for the next item
336
- $collectionHolder .data (' index' , index + 1 );
337
-
338
- // Display the form in the page in an li, before the "Add a tag" link li
339
- var $newFormLi = $ (' <li></li>' ).append (newForm);
340
- // Add the new form at the end of the list
341
- $collectionHolder .append ($newFormLi)
342
- }
343
-
344
- Code without jQuery library
345
-
346
- .. code-block :: javascript
347
- const newItem = (e ) => {
348
- const collectionHolder = document .querySelector (` .${ e .currentTarget .dataset .collectionHolderClass } ` );
349
-
350
- const item = document .createElement (" div" );
308
+ const item = document .createElement (' li' );
351
309
352
310
item .innerHTML = collectionHolder
353
311
.dataset
@@ -362,10 +320,6 @@ Code without jQuery library
362
320
collectionHolder .dataset .index ++ ;
363
321
};
364
322
365
- document
366
- .querySelectorAll (' .add_item_link' )
367
- .forEach (btn => btn .addEventListener (" click" , newItem));
368
-
369
323
Now, each time a user clicks the ``Add a tag `` link, a new sub form will
370
324
appear on the page. When the form is submitted, any new tag forms will be converted
371
325
into new ``Tag `` objects and added to the ``tags `` property of the ``Task `` object.
@@ -577,36 +531,35 @@ First, add a "delete this tag" link to each tag form:
577
531
578
532
.. code-block :: javascript
579
533
580
- jQuery (document ).ready (function () {
581
- // Get the ul that holds the collection of tags
582
- $collectionHolder = $ (' ul.tags' );
583
-
584
- // add a delete link to all of the existing tag form li elements
585
- $collectionHolder .find (' li' ).each (function () {
586
- addTagFormDeleteLink ($ (this ));
587
- });
588
-
589
- // ... the rest of the block from above
590
- });
534
+ const tags = document .querySelectorAll (' ul.tags' )
535
+ tags .forEach ((tag ) => {
536
+ addTagFormDeleteLink (tag)
537
+ })
591
538
539
+ // ... the rest of the block from above
540
+
592
541
function addFormToCollection () {
593
542
// ...
594
543
595
544
// add a delete link to the new form
596
- addTagFormDeleteLink ($newFormLi );
545
+ addTagFormDeleteLink (item );
597
546
}
598
547
599
548
The ``addTagFormDeleteLink() `` function will look something like this:
600
549
601
550
.. code-block :: javascript
602
551
603
- function addTagFormDeleteLink ($tagFormLi ) {
604
- var $removeFormButton = $ (' <button type="button">Delete this tag</button>' );
605
- $tagFormLi .append ($removeFormButton);
552
+ const addTagFormDeleteLink = (tagFormLi ) => {
553
+ const removeFormButton = document .createElement (' button' )
554
+ removeFormButton .classList
555
+ removeFormButton .innerText = ' Delete this tag'
556
+
557
+ tagFormLi .append (removeFormButton);
606
558
607
- $removeFormButton .on (' click' , function (e ) {
559
+ removeFormButton .addEventListener (' click' , (e ) => {
560
+ e .preventDefault ()
608
561
// remove the li for the tag form
609
- $ tagFormLi .remove ();
562
+ tagFormLi .remove ();
610
563
});
611
564
}
612
565
@@ -702,7 +655,7 @@ the relationship between the removed ``Tag`` and ``Task`` object.
702
655
703
656
.. _`Owning Side and Inverse Side` : https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/unitofwork-associations.html
704
657
.. _`jQuery` : http://jquery.com/
705
- .. _`JSFiddle` : http ://jsfiddle.net/847Kf/4 /
658
+ .. _`JSFiddle` : https ://jsfiddle.net/ey8ozh6n /
706
659
.. _`@a2lix/symfony-collection` : https://github.com/a2lix/symfony-collection
707
660
.. _`symfony-collection` : https://github.com/ninsuo/symfony-collection
708
661
.. _`ArrayCollection` : https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html
0 commit comments