Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

creating javascript objects in LCD 7.01

Avatar

Former Community Member
I'd like to create a validation class in a scripting object and give it several validation methods . After that I'd like to create an instance object of the class when the form loads and be able to set and get the properties of the new object and to access the methods of the object - from any field in the form.



So far I've got nowhere. I create a function in a scripting object sco, e.g.



function Test() {

this.myProp = "xyz";

}



Then in the initialise event of the form I try something like:



myTest = sco.Test;

app.alert("the value of myProp is " + myTest.myProp);



This gives me an alert box with 'Undefined'



or I try



myTest = new sco.Test; // notice the new keyword

app.alert("the value of myProp is " + myTest.myProp);



In this case the app.alert gets ignored.



If I create a method in my class, it gets worse - get message like 'Funciton undefined'.



Does LCD 7.01 support this style of coding or do I have stick with basic function calls?
5 Replies

Avatar

Former Community Member
So I think your issue is with the use of the "this" keyword in the script object. "this" is used to refernce the current field but when you use it inside of a script object then it has no context. If you executed the code outside of the script object then you would be OK.



So I suggest that you call the function from whatever object event you want like this:



sco.Test(this);



and then the function would be like this:



function Test(myObj) {

myObj.myProp = "xyz";

}



Note that in javascript you cannot create new objects ....if myProp refers to a new property you want to set then realize that you are restricted to properties of the object that you pass in. This is javascript not java.



Hope that helps

Avatar

Former Community Member
I can do basic function calls to the script object functions without problem.



I don't understand your last sentence. What I'd like to do is use basic Javascript Object techniques, e.g.










function Fn(country,people) {

this.country=country;

this.people=people;

this.view=view;

}

function view() {

with (this) document.write(country+' has '+people+' people'+'

');

}

testFn1=new Fn('US',300000000);

testFn2=new Fn('Oz',20000000);

testFn1.view();

testFn2.view();









except in my case I would like the function object to reside in my Designer script object and be able to access its properties and methods outside the script object, much in the same way that you call a function from outside the script object.



So instead of

sco.Test(this); \\ which I can do without problem



In the form initialise event I do



oValidator = new sco.Validator;



then in



each of my form fields I set some properties



oValidator.fn = 'ValidateDate';

oValidator.obj = this;

oValidator.literal = 'Part A Q5';



oValidator.DoIt();

Avatar

Former Community Member
sorry my example code disappeared



function Fn(country,people) {

this.country=country;

this.people=people;

this.view=view;

}

function view() {

with (this) document.write(country+' has '+people+' people'+'

');

}

testFn1=new Fn('US',300000000);

testFn2=new Fn('Oz',20000000);

testFn1.view();

testFn2.view();

Avatar

Level 4
You can create your own objects but there are a few quirks. Personally I haven't managed to get the new operator to work on constructor functions in PDF forms, but new Object() does. So exploiting this, you could modify that example code just posted as follows:



In script object named sco:



> function Fn(country,people) {



> var obj = new Object();



> obj.country=country;



> obj.people=people;



> obj.view=view;



> return obj;



>}



>function view() {



> with (this) xfa.host.messageBox(country+' has '+people+' people'+'');



>}



In form code (e.g. initialise, button click, etc.):



> var testFn1=sco.Fn('US',300000000);



> var testFn2=sco.Fn('Oz',20000000);



> testFn1.view();



> testFn2.view();



Far from ideal, but I hope this might help you achieve your goal.



I tested the above using Designer ES with target version 7.0.5.

Avatar

Former Community Member
thank you very much for your reply. Yes, it works! awesome.



As I suspect you already know, there is a great advantage in packaging code like this - using properties instead of parameter passing. It means that if you want to add an extra input then its a simple matter of adding a new property and setting it, if needed, prior to calling the function. To be contrasted with adding a parameter to a function which means that every function call has to be changed.



Not in every situation of course but in ones where the number of inputs could change quite a lot.