Page 1 of 1

Fire a "user-triggered" event in code - can this be done?

PostPosted: Fri Jan 03, 2020 2:18 pm
by c.groefsema
Hi everyone,

I need to fire an event from code that is normally triggered by a user action. For example, a control has an onEntryChanged(oldValue, newValue, event) event attached to it, which fires whenever the user changes its value.
Now its value is changed by code as well, by altering the dataProvider, and onEntryChanged doesn't fire in that case. Which makes sense. I need to have access to the event parameter though: onEntryChanged passes it on to other functions, which in turn pass it on to other functions... et cetera. I need it to exist as well when the value is changed by code, preferably by having onEntryChanged somehow fire all the same.
Calling onEntryChanged anyway and adding code to it that for example retrieves the relevant element name in a different way rather than reading the unavailable event.getElementName() in case event equals null - and pass that as an additional parameter to all those other functions - is much work, extremely ugly and error prone. In other words: no option.

Is there a way in code to mimick a user's action of changing a control's value so that relevant events fire, or is there another way to obtain the event parameter?

Thanks in advance!
Coos

Re: Fire a "user-triggered" event in code - can this be done

PostPosted: Fri Jan 03, 2020 4:37 pm
by ROCLASI
Hi Coos,

You could split out all the generic logic into it's own method and call that from the onEntryChanged event like so:
Code: Select all
/**
* @param oldValue
* @param newValue
* @param {JSEvent} event
*/
function onEntryChanged(oldValue, newValue, event) {
    return genericEntryChange(event.getElementName(), oldValue, newValue);
}

/**
* @param {String} elementName
* @param oldValue
* @param newValue
*/
function genericEntryChange(elementName, oldValue, newValue) {
    // your business logic here

    return true; // or false
}

This way you can call the same code from the event and from other code.

Hope this helps.

Re: Fire a "user-triggered" event in code - can this be done

PostPosted: Fri Jan 17, 2020 1:50 pm
by c.groefsema
Hi Robert,

Sorry for the late reply... I kind of forgot all about this topic since it became irrelevant for me.
I should have replied sooner - I appreciate your effort to help me.

Kind regards,
Coos