0

I am using Meteor, FlowRouter, and Parsley for validation. When I reload the app and enter into the page with the form, I get this error and parsley is not working on the form:

You must bind Parsley on an existing element.

If I leave the page and come back, it works fine. I have the following code initializing the binding:

Template.report.onRendered ->
   report = Reports.findOne(_id: FlowRouter.getParam('reportId'))

   if report.status == 'finalized'
      Session.set('showDistributeReport', true)
   else
      Session.set('showDistributeReport', false)

   $('#status-js').val(report.status)
   $('#report-form-js').parsley()

I have been using Parsley and haven't seen this problem on other pages. Any help would be greatly appreciate.

sturoid
  • 2,501
  • 4
  • 20
  • 36
  • You'll get that error when binding parsley on an empty set. Just check for that – Marc-André Lafortune Dec 07 '15 at 18:34
  • Hey @Marc-AndréLafortune the weird thing is that it works find after the first time the route is entered. Do you know how you check if the element is there and wait for it to render if it's not there yet? I thought that onRendered was supposed to do that but it doesn't seem to be working on that element all the time. – sturoid Dec 08 '15 at 06:24

1 Answers1

0

I figured out a fix but I'm not sure why I had to do this. If anyone has any input that would be great. I had to take the portion where I set the session variable and move it to the onCreated method. This is the code I have now.

Template.report.onCreated ->
   report = Reports.findOne(_id: FlowRouter.getParam('reportId'))

   if report.status == 'finalized'
      Session.set('showDistributeReport', true)
   else
      Session.set('showDistributeReport', false)


Template.report.onRendered ->
   report = Reports.findOne(_id: FlowRouter.getParam('reportId'))

   $('#status-js').val(report.status)
   $('#report-form-js').parsley()
sturoid
  • 2,501
  • 4
  • 20
  • 36