How to fix an issue with a method being executed too many times in a code schema section?

Hello community:



I'm defining a rule inside the section code schema to remove spaces from a field value when the user tries completes the field "UsrTexto1". It is defined as follows:

methods: {
    cambioTexto1: function() {
        var vueltas = this.get("Vueltas");
        this.set("Vueltas",vueltas+1);
 
        var txt1 = this.get("UsrTexto1");
        if (txt1.includes(' ')) {
            var txt2 = txt1.replaceAll(' ','');
            this.set("UsrTexto1",txt2);
        }
    },
}

So when the field is modified it searches for spaces inside the text and removes them. This is an example of execution:

1) When the user types the text value

2) After the method is executed:

 

The problem is that the function is executed too many times. I've added a counter 'vueltas' to show how many times the function is called and here it is the result:

It doesn't make sense as I think that the method should be called exactly 2 times (1st when it removes the spaces, and 2nd when the system detects new changes on the field and now there are no spaces). The main problem is that the whole operation is slowed down because of the high number of calls.

 

How can I fix this?

 

Thanks in advance!

Like 0

Like

5 comments

Hello,

Can you please clarify how exactly the cambioTexto1 method is called?

Thank you!

The issue is that every change made to the text results in it firing the function again, plus, the changed value won't be read by the function on subsequent calls due to the timing is that the function firing and the value changing, which is why it's getting called so many times (and likely exceeding out the call stack max).

 

Instead if firing the value on the change, maybe fire it on the blur (when the user exits the control). To do this, remove the change attribute for the control and add this to the values block for the control in the diff:

"blur": {
    "bindTo": "cambioTexto1"
}

Or you could do it on save before using callParent.

 

Ryan

Oleg Drobina,

Hello Oleg,

In the section page there is a field called "Texto1" (with the code UsrTexto1). Inside the code schema of that section is the method that I provided earlier.

As you may see the method is called cambioTexto1, which is compose of cambio ("change" in Spanish) and Texto1 (the name of the field). It means that Creatio internally calls that function each time the value of that field is modified (with a built-in trigger I guess).

Therefore, the method is always called automatically, I don't call it manually.

Thanks for your interest!

Ryan Farley,

That makes sense,



I will try that tomorrow and check if it solves the problem



Thanks for you help!

Ryan Farley,

Hi again,

 

I've tried with the solution you provided but I haven't been able to fix the issue yet.

 

I also took a look at and older post where you submitted the same solution. I've tried to follow the implementation as you suggest. Here I share the current code:

{
	"operation": "insert",
	"name": "STRINGe5cdab8a-f59c-4c1c-aa90-c412e4c3e5ae",
	"values": {
		"layout": {
			"colSpan": 24,
			"rowSpan": 1,
			"column": 0,
			"row": 1,
			"layoutName": "ProfileContainer"
		},
		"bindTo": "UsrTexto1",
		"enabled": true,
		"blur": {
			"bindTo": "cambioTexto1"
		}
	},
	"parentName": "ProfileContainer",
	"propertyName": "items",
	"index": 1
},

Note: I have also removed the attributes part as you mentioned.

The problem is that when I try to modify a register or create a new one this happens:

The page displays blank content and, as you can see on the browser console at the right, there is an error related to an undefined value.

The messages you see before the error are traces I added to see the variable values:

console.log("Vueltas: " + this.get("Vueltas"));
- Vueltas: 0
 
console.log("Modificado: " + this.get("Modificado"));
- Modificado: false
 
console.log("Value read: " + this.get("UsrTexto1"));
- Value read: undefined

I have already read the documentation for the client schemas and I still have no clue how to solve it.

 

Regards.

Show all comments