Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Adding a Namespace Object to the XFA Object

Avatar

Former Community Member

Hi,

I'm looking into optimizing the code in my forms. I noticed that, unlike most of the other LiveCycle Objects, the root XFA Object is enumerable and writable, and I was just wondering if anyone knows if it's safe to create my own Namespace Object and add it as a property of the XFA Object. If it is ok to do this, then it would be a great way to store all the form properties... all your Script Objects could point to the same location. I was able to get it to work using the following code:

//create global namespace object
var OBJ = ( function() {
          if ( typeof xfa.OBJ === "undefined" ) {
               xfa.OBJ = {};
          }
          return xfa.OBJ;
     }());


Thanks,

Anthony

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Anthony,

I've always thought of a script object as a namespace, I don't see the chances of a name collision happening with a PDF form.

Here is some more information about garbage collection, Scope of JavaScript Objects

I guess I've been avoiding updating the XFA objects since I read that post.

Regards

Bruce

View solution in original post

4 Replies

Avatar

Level 10

Hi Anthony,

I would worry about the property you add to an XFA object being garbage collected, see this from the help Adobe Digital Enterprise Platform * Enforcing strict scoping rules in JavaScript

You might be better off adding it to the Doc or app objects, that are more Acrobat than XFA, but I am not sure what you mean by a "namespace object", if you could explain more then there maybe other options.

Regards

Bruce

Avatar

Former Community Member

Thanks for the response Bruce.

Please look at the following link to get an idea of what I mean by namespace object:

A JavaScript Module Pattern - YUI Blog

I know this is more for browser based JavaScript, but I can see the benefits to using the idea within a form as well.

Avatar

Former Community Member

I was looking into the Doc and app objects that you suggested. I can't seem to add my own object to Doc, but I can to app. The only thing with app is that it is persistent.

The app object would be a great place to create a namespace object for storing properties and methods that are common to all my forms.

I was thinking that the xfa object would be a better place to store properties and methods specific to a particular form. Garbage collection doesn't seem to be an issue, but I could be wrong.

Here is a simple example of what I am doing, and it seems to work:

- add a makeMandatory() method to app.OBJ that accepts an array of form controls (persistent method used by all forms)

- add an array of form controls to xfa.OBJ when the form is initialized (array can be dynamic)

- when a button is clicked, run the following script: app.OBJ.makeMandatory(xfa.OBJ.mandatoryFormControls);

Avatar

Correct answer by
Level 10

Hi Anthony,

I've always thought of a script object as a namespace, I don't see the chances of a name collision happening with a PDF form.

Here is some more information about garbage collection, Scope of JavaScript Objects

I guess I've been avoiding updating the XFA objects since I read that post.

Regards

Bruce