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

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

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

Postby c.groefsema » Fri Jan 03, 2020 2:18 pm

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
User avatar
c.groefsema
 
Posts: 26
Joined: Fri Feb 22, 2019 12:40 pm

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

Postby ROCLASI » Fri Jan 03, 2020 4:37 pm

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.
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: Fire a "user-triggered" event in code - can this be done

Postby c.groefsema » Fri Jan 17, 2020 1:50 pm

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
User avatar
c.groefsema
 
Posts: 26
Joined: Fri Feb 22, 2019 12:40 pm


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 12 guests