How to remove "on change"?

Hi,
Is there a way to remove "on change" listener?
For example on start I set "on change" this way:

	fd.field('account').$on('change', function (value) {
		console.log('on_change account', value);
	});

but under some conditions I would like it to do nothing.
I tried to replace the "on change" function with doNothing(){} but initial function still works.

function doNothing() {
return
}

and:
fd.field('account').$on('change', doNothing() )

changes nothing. console still prints value of the field.

Regards
Marcin

Dear @Marcin,
Should be pretty straightforward, just add your condition inside of the function:

fd.field('account').$on('change', function (value) {
    //check condition and do nothing if true
    if(some condition) {
       return;
    }
    console.log('on_change account', value);
});

Well, that is not this kind of case.
The problem is that „on change” fires somehow during

If (fd.isValid){
  Do somethin()
  fd.save()
}

In my save function which is window.send = function (){}

This is the reason why I think about clearing this just before checking if form is valid.

Regards
Marcin

Dear @Marcin,
Still, not sure what prevents you from adding a condition. You can have a separate variable which will be changed when you don't want to fire the event:

var stopChange = false;
fd.spRendered(function(){
 fd.field('account').$on('change', function (value) {
    //check condition and do nothing if true
    if(stopChange) {
       return;
    }
    console.log('on_change account', value);
 });
});

Then you can then change before running this code:

if (fd.isValid){
  stopChange = true;
  Do somethin()
  fd.save()
}
1 Like

Well, that is good trick.
Key was using a bool variable! Thank you.
So my problem is solved.

But I still can not understand why "on change" on this particular fields is running in fd.isValid. It wouldn't even be that bad if it had run properly. But in my "checking" function I compare 3 values (3 persons to determine if I have at least 2 different among them - to keep 4 eye principle) and it happens all the time that in fired onchange values of all these fields were null.
So I have 3 fields: approver1, approver2 and approver3 set properly on the form. "On change" defined in all these fields verifies if 4 eye principle is met. And when user is filling the form everything works perfect. But to my "save and send" button I have assigned a function which also changes next status (string field), so first I check if the form is valid and if yes then save the form.

window.SendForApproval = function (next) {
	if (fd.isValid) { //somewhere here "onchange" is running and values of my tested fields are null for "onchange"
		fd.field('status').value = next;
		fd.save();
	} else {
		alert('Form is invalid!');
	}
};

So my additional questions are:

  1. Why "on change" is being executed?
  2. Why function defined in "on change" can not read proper values when running in or just after fd.isValid?

Regards
Marcin

Dear @Marcin,
It might be a bug, but I can't say with 100% certainty - it definitely doesn't happen with all the fields. Moreover, I don't think that it's triggered by fd.isValid - the more likely reason is that the change event is triggered on save, when all fields are cleared of value. Technically the field value changes to nothing, so it's a change and no value can be retrieved at the moment as it changes to nothing.