@@ -13,8 +13,21 @@ const People = {
1313 // Replace the placeholder index with the actual index
1414 newForm = $ ( newForm . replace ( / r e p l a c e - m e / g, index + 1 ) ) ;
1515 newForm . appendTo ( list ) ;
16- const nameInput = newForm . find ( '.person-name' ) ;
17- const orcidInput = newForm . find ( '.person-orcid' ) ;
16+
17+ People . bind ( newForm ) ;
18+
19+ return newForm ;
20+ } ,
21+
22+ delete : function ( ) {
23+ $ ( this ) . parents ( '.person-form' ) . fadeOut ( 'fast' , function ( ) {
24+ $ ( this ) . remove ( ) ;
25+ } ) ;
26+ } ,
27+
28+ bind : function ( element ) {
29+ const nameInput = element . find ( '.person-name' ) ;
30+ const orcidInput = element . find ( '.person-orcid' ) ;
1831 const opts = {
1932 orientation : 'top' ,
2033 triggerSelectOnValidInput : false ,
@@ -32,19 +45,11 @@ const People = {
3245 formatResult : Autocompleters . formatResultWithHint
3346 }
3447
35- opts . serviceUrl = list . parents ( '[data-role="people-form"]' ) . data ( 'autocompleteUrl' ) ;
48+ opts . serviceUrl = element . parents ( '[data-role="people-form"]' ) . data ( 'autocompleteUrl' ) ;
3649 opts . dataType = 'json' ;
3750 opts . deferRequestBy = 100 ;
3851
3952 nameInput . autocomplete ( opts ) ;
40-
41- return newForm ;
42- } ,
43-
44- delete : function ( ) {
45- $ ( this ) . parents ( '.person-form' ) . fadeOut ( 'fast' , function ( ) {
46- $ ( this ) . remove ( ) ;
47- } ) ;
4853 } ,
4954
5055 init : function ( ) {
@@ -70,8 +75,14 @@ const People = {
7075 nextItem . find ( 'input.form-control:first' ) . focus ( ) ;
7176 }
7277 } ) ;
78+
79+ // Set up autocomplete on any existing fields
80+ form . find ( '.person-form' ) . each ( function ( node ) {
81+ People . bind ( $ ( this ) ) ;
82+ } ) ;
83+
84+ $ ( form ) . on ( 'change' , '.delete-person-btn input.destroy-attribute' , People . delete ) ;
7385 } ) ;
7486
75- $ ( '.delete-person-btn input.destroy-attribute' ) . change ( People . delete ) ;
7687 }
7788} ;
0 commit comments