mod_datejs_init() Problem

Questions, tips and tricks and techniques for scripting in Servoy

mod_datejs_init() Problem

Postby kwpsd » Fri May 04, 2012 2:32 am

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;
}
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: mod_datejs_init() Problem

Postby omar » Thu Jun 28, 2012 4:08 pm

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?
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: mod_datejs_init() Problem

Postby kwpsd » Thu Jun 28, 2012 6:36 pm

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.
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: mod_datejs_init() Problem

Postby omar » Fri Jun 29, 2012 10:18 am

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.
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: mod_datejs_init() Problem

Postby kwpsd » Sat Jun 30, 2012 3:15 am

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!
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: mod_datejs_init() Problem

Postby ROCLASI » Sat Jun 30, 2012 10:07 am

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: mod_datejs_init() Problem

Postby ROCLASI » Sat Jun 30, 2012 11:05 am

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: mod_datejs_init() Problem

Postby ROCLASI » Sat Jun 30, 2012 2:25 pm

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: mod_datejs_init() Problem

Postby kwpsd » Sat Jun 30, 2012 10:05 pm

Hi, Robert.

Thank you so much for looking into this and cleaning up the code...I really appreciate your efforts!
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA


Return to Methods

Who is online

Users browsing this forum: No registered users and 4 guests