Expand my Community achievements bar.

SOLVED

creating javascript objects in LCD 8.2

Avatar

Level 4

Hi,

a year ago there was a posting "creating javascript objects in LCD 7.0.1 " http://forums.adobe.com/message/1360679#1360679

I tried this in Designer 8.2 and failed! Does anyone know why?

I set up a scriptObject "mydate" which contains:


function createDate(sDay, sMonth, sYear) {
    xfa.host.messageBox( sDay + "." + sMonth + "." + sYear);
    var oDateObject = new Object();
    oDateObject .day = sDay;
    oDateObject .month = sMonth;
    oDateObject .year = sYear;
    return oDateObject;
    } // function createDate()

function setToCurrentDate () {
    var dateToday = new Date();
    this.day = (dateToday.getDate() < 10) ? "0" + dateToday.getDate() : dateToday.getDate();
    this.month = (dateToday.getMonth() < 10) ? "0" + dateToday.getMonth() : dateToday.getMonth();
    this.year = 1900 + dateToday.getYear();
    } // setToCurrentDate ()
   
function display() {
    xfa.host.messageBox("Display: " + this.day + "." + this.month + "." + this.year);
    return "" + this.day + "." + this.month + "." + this.year ;
    }
   
function display2() {
    with (this) xfa.host.messageBox("Display: " + day + "." + month + "." + year);
    }

In my field's Enter-Event I wrote:

    var dateToday = mydate.createDate("01","01","2000");
    dateToday.display2();
    xfa.host.messageBox(dateToday.display());
    dateToday.setToCurrentDate();

The problem is, that I get the message: "dateToday.display2 is not a function"

I cannot call any of dateToday's methods.

Actually I would have expected this message, as there is no scriptObject with the name "dateToday". But apparently that worked in version 7.0.1.

How can we use some object oriented programming with Adobe's JavaScript?

How can we use objects in the scriptObjects?

Did anyone find that out already? I'd appreciate every hint and help!

Ulrich

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Ulrich

I don't think the Designer Script Objects are implemented as JavaScript objects but I think you can acheive what you want with something like;

form1.#variables[0].myDateScript - (JavaScript, client)

function myDate(sDay, sMonth, sYear)

{

    this.day = sDay;

    this.month = sMonth;

    this.year = sYear;

    this.setToCurrentDate = function(oDateObject)

    {

         this.day = (dateToday.getDate() < 10) ? "0" + dateToday.getDate() : dateToday.getDate();

         this.month = (dateToday.getMonth() < 10) ? "0" + dateToday.getMonth() : dateToday.getMonth();

         this.year = 1900 + dateToday.getYear();  

     }

     this.display = function()

     {

         return "" + this.day + "." + this.month + "." + this.year ;

     }

}

function newMyDate(sDay, sMonth, sYear)

{

     return new myDate(sDay, sMonth, sYear);

}

So you could then reference the myDate object with;

xfa.host.messageBox(myDateScript.newMyDate(

"01","01","2000").display());

John Brinkman has a post on this at http://blogs.adobe.com/formfeed/2009/09/script_objects_deep_dive.html and before that post my previous method was discribed in this forum at http://forums.adobe.com/message/1964601

Also attached is my sample to test the code above.

Bruce

View solution in original post

3 Replies

Avatar

Level 4

a very ugly makeshift solution is of course: Writing in the scriptObject "mydate"

function createDate(sDay, sMonth, sYear) {
    var oDateObject = new Object();
    oDateObject.day = sDay;
    oDateObject.month = sMonth;
    oDateObject.year = sYear;
    return oDateObject;
    } // function createDate()

function setToCurrentDate (oDateObject) {
    var dateToday = new Date();
    oDateObject.day = (dateToday.getDate() < 10) ? "0" + dateToday.getDate() : dateToday.getDate();
    oDateObject.month = (dateToday.getMonth() < 10) ? "0" + dateToday.getMonth() : dateToday.getMonth();
    oDateObject.year = 1900 + dateToday.getYear();
    } // setToCurrentDate ()
   
function display(oDateObject) {
    return "" + oDateObject.day + "." + oDateObject.month + "." + oDateObject.year ;
    }

And use that stuff in the event with

    var dateToday = mydate.createDate("01","01","2000");
    mydate.setToCurrentDate(dateToday);
    this.rawValue = mydate.display(dateToday);

Better than nothing. But certainly not a properly encapsulated class with all the possible drawbacks.

Thus: I am still looking for the right solution of my problem

Avatar

Correct answer by
Level 10

Hi Ulrich

I don't think the Designer Script Objects are implemented as JavaScript objects but I think you can acheive what you want with something like;

form1.#variables[0].myDateScript - (JavaScript, client)

function myDate(sDay, sMonth, sYear)

{

    this.day = sDay;

    this.month = sMonth;

    this.year = sYear;

    this.setToCurrentDate = function(oDateObject)

    {

         this.day = (dateToday.getDate() < 10) ? "0" + dateToday.getDate() : dateToday.getDate();

         this.month = (dateToday.getMonth() < 10) ? "0" + dateToday.getMonth() : dateToday.getMonth();

         this.year = 1900 + dateToday.getYear();  

     }

     this.display = function()

     {

         return "" + this.day + "." + this.month + "." + this.year ;

     }

}

function newMyDate(sDay, sMonth, sYear)

{

     return new myDate(sDay, sMonth, sYear);

}

So you could then reference the myDate object with;

xfa.host.messageBox(myDateScript.newMyDate(

"01","01","2000").display());

John Brinkman has a post on this at http://blogs.adobe.com/formfeed/2009/09/script_objects_deep_dive.html and before that post my previous method was discribed in this forum at http://forums.adobe.com/message/1964601

Also attached is my sample to test the code above.

Bruce

Avatar

Level 4

Hi John,

thank you very much for that perfect solution.

To make your code ready to use for everyone and hopefully even more understandable I added a few lines.

The following is the code of the "class" in a scriptObject:


Formular1.#variables[0].myDateScript - (JavaScript, client)


function myDate(sDay, sMonth, sYear) {
    this.day = sDay;
    this.month = sMonth;
    this.year = sYear;
   
    this.setToCurrentDate = function() {
        var dateToday = new Date();
        this.day   = (dateToday.getDate() < 10) ? "0" + dateToday.getDate() : dateToday.getDate();
        this.month = (dateToday.getMonth() < 10) ? "0" + dateToday.getMonth() : dateToday.getMonth();
        this.year  = 1900 + dateToday.getYear();
        } // setToCurrentDate ()


    this.display = function() {
        return "" + this.day + "." + this.month + "." + this.year ;
        } // display()
   
    } // function myDate()
   
function createMyDate(sDay, sMonth, sYear) {
    return new myDate(sDay, sMonth, sYear);
    } //newMyDate()
 

And here is how I call it:

    var oDateToday = myDateScript.createMyDate("01","01","2000");
    oDateToday.setToCurrentDate();
    this.rawValue = oDateToday.display();

It is of course ugly to write (as you mentioned). So what. We have the functionality of a class which we can easily pop into our forms without worrying about interferences with the rest of the code.

I certainly bookmarked your blog!

Thanks again.