Expand my Community achievements bar.

SOLVED

addInstance in loop

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Level 10

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

this.instanceManager.setInstances(number);

or

this.instanceManager.count = number;

View solution in original post

6 Replies

Avatar

Level 10

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

Avatar

Correct answer by
Level 10

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

this.instanceManager.setInstances(number);

or

this.instanceManager.count = number;

Avatar

Level 2

Thanks Niall for the quick respone!

There is no specific reason to add this code in the initialize event. I just figured when the form is opened I need to add my instances.

Avatar

Level 2

Thanks Jono,

I will try this. It's better not to use a loop. I didn't know the setInstances was available. Can I use this on my initialize event? Or do I need to use the docReady event?

I'll try this first thing on Monday because today (and the weekend) I don't have access to the laptop on which I create my form.

Avatar

Level 10

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

Avatar

Level 2

Thanks guys! The setInstance() did the job.

The intialize event explanation will be very usefull in the future.