Stop updataRow() from calling its own change event?

  • 1
  • Question
  • Updated 3 years ago
  • Answered
I have this function within a custom component to handle changes. I want to run updateRow() within the function, but I end up with a loop that runs several times before mysteriously quitting.

Does a function have an initiatorId? Or is there anythingother way to tell the function not to repeat itself after it calls updateRow()?

// Define a function to handle change.        
component.change = function(){
   console.log('CHANGE');
    // Do some other stuff...
                // Update Name and Date
var sigName = signatureModel.getFieldValue(signatureRow,'Signature_Name__c') || (defaultName == 'Custom') ? customDefaultName : defaultName;
        signatureModel.updateRow(
    signatureRow,
    {
      Signature_Name__c:  sigName,
      Signature_Date__c:  skuid.time.getSFDateTime(new Date())
    },
    {initiatorId: this._GUID} );
return component;
}
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
How is component.change being fired? Are you listening to the row.updated event from the skuid.events api? If so, you could probably filter it down to only listen to changes to certain fields, not all fields.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
component.change is handling changes for the editor:

component.editor.handleChange = component.change;
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Try adding a parameter to your change function and logging that.  It might have some info that you could use.

component.change = function(changeinfo){
   console.log('CHANGE');
   console.log(changeinfo);
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Here's what I get.
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
It looks like you're not getting any loop at all, just that there are 3 times where that row is updated in your component.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Sorry, that was a bad example... I had a different error in my code so it wasn't functioning properly.

Here's a better example:


The first CHANGE is called (as it should be) when I make a change in jsignature.
The second CHANGE is called because of the updateData within the first CHANGE.

I was thinking that I didn't need that second CHANGE. but perhaps I do?
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Yeah, I'm saying you can add an additional if statement around your updateRow call. Only updateRow if changeinfo.SignatureData__c != null. That way you won't get the possibility of an infinite loop.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Right. That makes sense.

Interestingly, when I pause the code and step through it in the console, CHANGE repeats infinitely. Everytime it runs updateRow() it immediately starts component.change() again. But when I just let it run by itself, it only runs component.change() once for each set of changes.
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Yeah, I know why that is. updateRow has some checks in it to prevent infinite loops. If you send in the exact same "payload" to updateRow, it will not update, or call any of its handlers. In your case, you're sending in the current date. So when you slowly iterate through using a debugger, it gets a different date each time. (Because it might be a second or millisecond off). But when you do it without the debugger, Javascript executes so fast that updateRow gets the exact same "payload" and refuses to notify its handlers.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Nice!

Still, worth a few extra lines of code to explicitly prevent the loop.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Thanks again, Ben. Got it working.