Help with JSEvent Please

Hi,

I curently doing a test upgrade of a 3.5 solution to 5, an would like some help with the new JSEvent implementation.

I have the following code:

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean} valid value
 *
 * @properties={typeid:24,uuid:"1915B6CA-0BC6-47EC-8C9D-2924FA5160B8"}
 */
function onDataChange(oldValue, newValue, event)
{
//************************************************************************************************************************
//get element trigger name
//************************************************************************************************************************
var vElemName = application.getMethodTriggerElementName()

//************************************************************************************************************************
//arguments from ondatachange 
//************************************************************************************************************************
	//TODO WARNING: do rewrite your code to not depend on 'arguments', append them to the parameter list.
var voldData = arguments[0] //old data value before change
var vnewData = arguments[1] //new data value after change

globals.gNewJournalRecord(quotesid,voldData,vnewData,0,3); //write to journal take pk and table name

databaseManager.saveData()

}

Is below correct ?, and how do I get the elementname ?, sorry if this is a stupid question…but a example of how I should recode would be of great benefit, and appreciated.

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean} valid value
 *
 * @properties={typeid:24,uuid:"1915B6CA-0BC6-47EC-8C9D-2924FA5160B8"}
 */
function onDataChange(voldData, vnewData, JSEvent.DATACHANGE)
{
//************************************************************************************************************************
//get element trigger name
//************************************************************************************************************************
var vElemName = application.getMethodTriggerElementName()

globals.gNewJournalRecord(quotesid,voldData,vnewData,0,3); //write to journal take pk and table name

databaseManager.saveData()

}

Hi Phil!

You code should rather look like this:

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean} valid value
 *
 * @properties={typeid:24,uuid:"1915B6CA-0BC6-47EC-8C9D-2924FA5160B8"}
 */
function onDataChange(voldData, vnewData, event)
{
//************************************************************************************************************************
//get element trigger name
//************************************************************************************************************************
var vElemName = event.getElementName();

globals.gNewJournalRecord(quotesid,voldData,vnewData,0,3); //write to journal take pk and table name

databaseManager.saveData();

return true;

}

BTW, I don’t know why do you want the elementName in this case, you don’t seem to use it? ;-)

And why don’t you let Servoy create the method for you, and use code completion on the parameters?
You have changed the event variable name in the parameters of the method to a constant, don’t do that.

Double click on the onDataChange event on the property of your element, click create a form method, change its name eventually, you will have the correct parameters (don’t change them)…

Then if you type event, then a dot (.) then (ctrl+SPACE), Serclipse will show you the methods available for the JSEvent type.

ptalbot:
Hi Phil!

You code should rather look like this:

/**
  • Handle changed data.
  • @param {Object} oldValue old value
  • @param {Object} newValue new value
  • @param {JSEvent} event the event that triggered the action
  • @returns {Boolean} valid value
  • @properties={typeid:24,uuid:“1915B6CA-0BC6-47EC-8C9D-2924FA5160B8”}
    /
    function onDataChange(voldData, vnewData, event)
    {
    //
    ***********************************************************************************************************************
    //get element trigger name
    //************************************************************************************************************************
    var vElemName = event.getElementName();

globals.gNewJournalRecord(quotesid,voldData,vnewData,0,3); //write to journal take pk and table name

databaseManager.saveData();

return true;

}



BTW, I don't know why do you want the elementName in this case, you don't seem to use it? <img src="{SMILIES_PATH}/icon_wink.gif" alt=";-)" title="Wink" />

And why don't you let Servoy create the method for you, and use code completion on the parameters? 
You have changed the event variable name in the parameters of the method to a constant, don't do that.

Double click on the onDataChange event on the property of your element, click create a form method, change its name eventually, you will have the correct parameters (don't change them)...

Then if you type event, then a dot (.) then (ctrl+SPACE), Serclipse will show you the methods available for the JSEvent type.

Thanks Patrick,

I have updated my code as you suggested and now it works. Looking at the documentation for JSEvent can I assume that lots of other parameters are available and you access them like below ??

var vElemName = event.getElementName();
var vType = event.getType();
etc…etc

Question 2

I have quite a few global methods in 3.5 where I meet certain conditions based on either the application.getMethodTriggerElementName(), or if that is ‘null’ ,I just pass the element name as a argument from some other method. Hope that makes sense ??

Would the below code replicate that, do you think ??

if (event.getType() == JSEvent.ACTION) //see if button was pressed
{
   var vElemName = event.getElementName(); //get element name from pressed button
} else {
   var vElemName = event; //just get the button name passed from method as argument[0]
}

//************************************************************************
//dispatch department
//************************************************************************
if(vElemName == 'btn_goDispatch'){ //goto dispatch department
//do something
}
		
//************************************************************************
//invoice department
//************************************************************************
if(vElemName == 'btn_goInvoice'){ //goto invoice department
//do something else
}

Many Thanks

wvitpr:
I have updated my code as you suggested and now it works. Looking at the documentation for JSEvent can I assume that lots of other parameters are available and you access them like below ??

var vElemName = event.getElementName();
var vType = event.getType();
etc…etc

YES!

wvitpr:
Question 2

I have quite a few global methods in 3.5 where I meet certain conditions based on either the application.getMethodTriggerElementName(), or if that is ‘null’ ,I just pass the element name as a argument from some other method. Hope that makes sense ??

Would the below code replicate that, do you think ??

if (event instanceof JSEvent && event.getType() == JSEvent.ACTION) //see if button was pressed

{
var vElemName = event.getElementName(); //get element name from pressed button
} else {
var vElemName = event; //just get the button name passed from method as argument[0]
}

If you use the same method from a button or from a call with a String, you should first check that the argument is indeed a JSEvent - thus event instanceof JSEvent - otherwise you will have and exception when invoking getType() on a String.

Best option is to set all your methods coming from buttons or events acts as proxy methods, extracting the values you need and calling another method where you put your real process which will not need to check its parameter:

function onAction(event) {
   var vElemName = event.getElementName();
   doAction(vElementName);
}
function doAction(name) {
   // your process here, name will always be a String!
}

Got it?