Skip navigation
nvdm1
Currently Being Moderated

addInstance in loop

Jun 28, 2012 8:46 AM

Tags: #dynamic #form #loop #addinstance #endless

Dear adobe experts,

 

I have a problem with the addInstance property.

 

I need to add a variable number of instances of a subform.

The code I've written in JavaScript on the initialize event is:

 

//  add needed nr of instances to the screen.

var length = xfa.record.INFRASTR.nodes.length/21;

length = Math.floor(length);

for (i=0;i<length;i++)

{

       this.instanceManager.addInstance(1);

}

 

When I run this code for var length = 1( after rouding down), this works perfect and 1 instance of the subform is added to the form. However as soon as the var length contains a value => '2' (meaning the loop is executed at least a second time) my entire form seems to be stuck in an endless loop.

 

This subform is set to flowed and the repeat subform flag is checked. The entire form is marked as a dynamic form. So I think all the necessary parameter for a repeatable subform are set.

 

Can anyone help me solve this problem?

 

Kind regards,

Niels

 
Replies
  • Currently Being Moderated
    Jun 28, 2012 8:52 AM   in reply to nvdm1

    Hi,

     

    The problem is the event that you have used. The initialize event will fire when the new instance of the repeating object is rendered. Therefore the script fires repeatedly.

     

    If you need this to fire when the form is opened, try docReady (with an if statement I suspect). Otherwise elaborate on why you need this script in the initialize event.

     

    Niall

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 28, 2012 9:44 AM   in reply to nvdm1

    Also, instead of a loop you can just set the number of instances.

     

    this.instanceManager.setInstances(number);

    or

    this.instanceManager.count = number;

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 29, 2012 4:10 PM   in reply to nvdm1

    Hi,

     

    First off, Jono is right, the setInstance() is another method that would work very well here.

     

    Because you have the script in the repeating object's initialize event (I can see that from the use of "this"), it means that it fires each time a new instance is created. This is what it causing the infinite loop.

     

    Script fires:

         > creates a new instance:

              > which causes initialize event to fire:

                   > creates a new instance:

                        > etc...

     

    If you want something to happen when the form is opened then the docReady event is a good choice. However be warned, while this event normally only fires once, if the form is Reader Enabled (at least through Acrobat), then the event fires twice.

     

    See discussions here:

     

    http://forums.adobe.com/message/3028586#3028586

     

    http://forums.adobe.com/message/2620119#2620119

     

    Also I would think that you would need to wrap the script in an if statement. Otherwise every time the form is opened, it is going to add more instances.

     

    Hope that helps,

     

    Niall

     

     

    Testing links...

     

    Message was edited by: Niall O\'Donovan

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points