Page 1 of 1

mod_datejs_init() Problem

PostPosted: Fri May 04, 2012 2:32 am
by kwpsd
mod_datejs() Version 0.6.1
Servoy Version 6.0.6

I believe there is something fundementally wrong with the 'mod_datejs' initialization code logic and would like a second opinion before posting this on Servoy Forge.

Note that the first line of the initialization method 'mod_datejs_init()' calls another method called 'globals.mod_datejs_isEnvExtended()':

Code: Select all
if ( globals.mod_datejs_isEnvExtended() ) return false;


Also, note that the first line of the 'mod_datejs_init()' method tests for the existence of the global variable 'ModDateJs':

Code: Select all
if ( typeof ModDateJs == 'undefined' ) return false;


Since 'ModDateJs' is defined globally, the test:

Code: Select all
typeof ModDateJs == 'undefined'


returns 'false' causing the 'if' statement to fall through, thus 'function mod_datejs_isEnvExtended()' returns 'true'.

This return value of true causes the 'if' statement in the initialization code to return 'false', thus the second line of code:

Code: Select all
globals.mod_datejs_load();


never gets executed, and the 'mod_datejs' methods cannot be found by the calling methods. This was the case in our solution.

We found that commenting out the first line of code in the initialization method causes the 'mod_datejs' methods to load and work.

Full code:

Code: Select all
/**
* @type {String}
*
* @properties={typeid:35,uuid:"85220A71-4B03-4E5E-8F7C-99D1273B8642",variableType:12}
*/
var ModDateJs = null;


/**
* @properties={typeid:24,uuid:"f333f53c-70c7-4f0f-943b-56c0e2078539"}
*/
function mod_datejs_init()
{
    if ( globals.mod_datejs_isEnvExtended() ) return false;
   
    globals.mod_datejs_load();
   
    ModDateJs = { version: globals.mod_datejs_version() };
   
    return true;
}

/**
* @properties={typeid:24,uuid:"0bc4ddca-19aa-4573-b7e1-138ba13047f7"}
*/
function mod_datejs_isEnvExtended()
{
    if ( typeof ModDateJs == 'undefined' ) return false;
   
    return true;
}

Re: mod_datejs_init() Problem

PostPosted: Thu Jun 28, 2012 4:08 pm
by omar
Hi,

I was looking through the unanswered posts and got curious by this one. I was able to reproduce the behaviour you describe and think the root of the problem lies in this line of code:

Code: Select all
if ( typeof ModDateJs == 'undefined' ) return false;


I can't help but wonder why you test for this if you have defined it as a global variable. Besides that even when you remove the variable definition from globals and ignore syntax warnings then it still doesn't change anything. My assumption is that the variable is defined (by Servoy) on the fly because it definitely exists when you test for it.

It's almost like quantum mechanics, if you look at something it changes behaviour. :mrgreen:

Guys, anybody recognize this fenomenon?

Re: mod_datejs_init() Problem

PostPosted: Thu Jun 28, 2012 6:36 pm
by kwpsd
Quantum software development?
It's almost like quantum mechanics, if you look at something it changes behaviour.


Omar, thanks for the verification. I am surprised no one else has come across this (or, complained about it). I will post it on Servoy Forge as a cautionary note.

Re: mod_datejs_init() Problem

PostPosted: Fri Jun 29, 2012 10:18 am
by omar
I think the way of testing is rather unusual. Normally if you don't know if a valid property or method exists (like in a drag operation) you test it something like this:

Code: Select all
if (element.getElementType && element.getElementType() == ELEMENT_TYPES.LABEL) {
   <action code>
}


But then again, you probably have good reasons for defining (and not initializing) the variable on the fly.

Re: mod_datejs_init() Problem

PostPosted: Sat Jun 30, 2012 3:15 am
by kwpsd
I agree with your statements...but, it is not my code. It's the existing code. I was hoping the code originator would comment and, also, to make others aware of the problem. Thank you for responding!

Re: mod_datejs_init() Problem

PostPosted: Sat Jun 30, 2012 10:07 am
by ROCLASI
Hi Kim,

The original author (Greg Pierce) is doing mostly iOS development now (Terminology, Phrasology, Drafts, etc.) however I am contributor to the project so I will take a good look at this.
If you already have a patch that works you are welcome to post that in a ticket on the project as well.

Re: mod_datejs_init() Problem

PostPosted: Sat Jun 30, 2012 11:05 am
by ROCLASI
Hi Kim,

The following piece of code you posted here is not in the original code so it seems you (or others) added that.
Code: Select all
/**
* @type {String}
*
* @properties={typeid:35,uuid:"85220A71-4B03-4E5E-8F7C-99D1273B8642",variableType:12}
*/
var ModDateJs = null;

See https://www.servoyforge.net/projects/mo ... globals.js

The function 'mod_datejs_init' declares the variable without using a var so this makes it a (true) JavaScript global.
Code: Select all
function mod_datejs_init()
{
    if ( globals.mod_datejs_isEnvExtended() ) return false;
   
    globals.mod_datejs_load();
   
    ModDateJs = { version: globals.mod_datejs_version() };
   
    return true;
}


I guess it can be rewritten to use a Servoy global but the way it is now is completely valid JS.

Hope this helps.

Re: mod_datejs_init() Problem

PostPosted: Sat Jun 30, 2012 2:25 pm
by ROCLASI
Okay, I released a new version of the module.
I cleaned up the code a bit and made it auto-initialising so you don't have to call the init method anymore (it's still there but deprecated).
Also got rid of some JS warnings, but some still remain.

You can get this new version (0.6.2) from ServoyForge.
https://www.servoyforge.net/projects/mod-datejs/files

It's not in the SVN (yet). Need to get access from Greg to be able to commit it.

Enjoy.

Re: mod_datejs_init() Problem

PostPosted: Sat Jun 30, 2012 10:05 pm
by kwpsd
Hi, Robert.

Thank you so much for looking into this and cleaning up the code...I really appreciate your efforts!