Skip to content
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

Select2 autoform creates an infinite loop and it's not reactive #70

Open
Lidofernandez opened this issue Apr 8, 2017 · 0 comments
Open

Comments

@Lidofernandez
Copy link

I have one form that includes two more forms and I need that when the second form is updated to show that information in the first Form this way:

  1. main form:
{{#autoForm collection=company doc=company id='mainForm' type=formType}}
 {{> uploadLogoForm }}
 {{> afQuickField
   name='company.logoUrl'
   class='fu-select-tag--background-image'
   label='Select a logo'
   type='select'
   options=logoOptions
   template='plain'}}
{{/autoform}}
  1. second form:
 <template name="uploadLogoForm">
  {{#autoForm collection=collection id="newLogoForm" type="normal" class="fu-form-upload"}}
    {{> afQuickField
     name="_id"
     accept=".png,.gif,.svg"
     label="Upload a logo"
     type="slingshot"
     directive="logos"}}
  {{/autoForm}}
</template>

So if I Upload a logo in the uploadLogoForm this logo should show/selected in the main form with this helper:

Template.mainForm.helpers({
 logoOptions() {
   const company = getCompany();
   const logoUrls = [];
   let currentLogoUrl;

   if (company) {
    currentLogoUrl = company.company.logoUrl;
   }

   const uploadedLogo = AutoForm.getFieldValue('_id', 'newLogoForm');
   if (uploadedLogo) {
    currentLogoUrl = getPath(uploadedLogo);
   }

   if (currentLogoUrl) {
    logoUrls.push(currentLogoUrl);
   }

   if (FlowRouter.subsReady('logos')) {
     Logo.find({}, {sort: {_id: 1}}).forEach(logo => {
       const logoUrl = getPath(logo._id);

       if (!_.include(logoUrls, logoUrl)) {
         logoUrls.push(logoUrl);
       }
     });
  }

  return logoUrls.map(logoUrl => ({
   label: logoUrl,
   value: logoUrl,
   selected: logoUrl === currentLogoUrl ? true : null
 }));
}});

This works nicely with if the type of the quickfield is 'select' But when I use 'select2' a lot of weird things happens like infinite loop in the helper and the select element is empty. I have to select the element or file manually in order to to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants