mod_datejs_init() Problem

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()’:

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’:

if ( typeof ModDateJs == 'undefined' ) return false;

Since ‘ModDateJs’ is defined globally, the test:

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:

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:

/**
 * @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;
}

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:

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?

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.

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:

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.

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!

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.

Hi Kim,

The following piece of code you posted here is not in the original code so it seems you (or others) added that.

/**
* @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.

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.

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.

Hi, Robert.

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