Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Howto return a new object from a script object

Avatar

Level 2

Hello,

I'm wondering about the fact that it seems not able to return a new object from an existing script object into a LiveCycle Designer Field.

Let's say we have follwing situation:

1. A Script object named "LcdObjects" with an object "Field":

function Field(som) {
this.som = som;

this.hideField = function() {
  som.presence = "hidden";
}
}

2. Now i want to create a new instance of the "Field" object within an existing text field (in the "click" event):

f = new LcdObjects.Field(this);
f.hideField();

3. The above code does not work with the text field. It seems that there is no new object created.

4. It seems that i have to create an auxiliary function for every object in the script object to get a successful return of a new object:

function newField(som) {
    return new Field(som);
}

5. With the auxiliary function i can create a new instance of the Field object within my text field:

f = LcdObjects.newField(this);
f.hideField();

But i'm not very happy with this solution because with this workaround i have to create an auxiliary return function for every single object which seems not very smart to me.

Is there no general soltution to return an instance of an object without using this kind of workaround?

Thank you.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

You can add a function to a script object that returns your "Field" object, with the hideField method. So a function like;

function Field(field)

{

    var that = { "field": field };

    that.hideField = function()

    {

        this.field.presence = "hidden";

    }

    return that;  

}

Could be called by

LcdObjects.Field(TextField1).hideField();

Regards

Bruce

View solution in original post

4 Replies

Avatar

Level 8

This might be easier:

In the initialize event (of any object, I pick the root subform) you can insert this:

Object.prototype.hideField = function(){

if (this.className=="field")

  this.presence="hidden";

}

then use this in your field without requiring a constructor at at:

  

this.hideField();

Kyle

Avatar

Level 2

This looks like a good solution, thank you.

But i'm still wondering if there might be a way to trigger/create the objects centralized from a script object, maybe like a self-invoking function:

 

(

Object.prototype.hideField = function(){

  if (this.className == "field") {

     this.presence = "hidden";

  }

})();

But till now i can't find a way to auto trigger functions from the script object at the initializing of the form.

Avatar

Correct answer by
Level 10

Hi,

You can add a function to a script object that returns your "Field" object, with the hideField method. So a function like;

function Field(field)

{

    var that = { "field": field };

    that.hideField = function()

    {

        this.field.presence = "hidden";

    }

    return that;  

}

Could be called by

LcdObjects.Field(TextField1).hideField();

Regards

Bruce

Avatar

Level 2

Hi Bruce,

thank you, that was what i'm looking for, it returns a new object directly from the script object.