Waiting for Dependent actions

Waiting for Dependent actions

anotherlawanotherlaw Posts: 7Questions: 4Answers: 0

I've implemented the editor update in the background....

editor
  .edit( rowIdx, false ) 
  .set( 'fieldName', value )
  .submit();

All works fine when 'fieldName' has no dependent actions.

However, if 'fieldName' does has dependent actions (editor.dependent()) then it would appear that neither .set() nor .submit() await the completion of those actions before the .submitI() is processed.

The background actions do properly trigger and I can trace the editor object being updated - but the database never gets updated.

editor.dependent('fieldName', function ( val, data, callback ) {
  console.log('  - Sub 1');
  editor.field('anotherField').set(anotherValue);
  console.log('  - Sub 2');
});


console.log('Main 1');
editor
  .edit( rowIdx, false ) 
  .set( 'fieldName', value )
  .submit();
console.log('Main 2');

generates the following output

Main 1
Main 2
- Sub 1
- Sub 2

After Sub 2 has completed the editor object IS in the correct state, but it is never submitted.

I've got a huge amount of dependent logic... so this is a real pain .... so I tried separating the process with a 'wait' inbetween

const sleep = (milliseconds) => {
      return new Promise(resolve => setTimeout(resolve, milliseconds))
    }

editor.dependent('fieldName', function ( val, data, callback ) {
  console.log('  - Sub 1');
  editor.field('anotherField').set(anotherValue);
  console.log('  - Sub 2');
});


console.log('Main 1');
editor
  .edit( rowIdx, false ) 
  .set( 'fieldName', value );
console.log('Main 2');

sleep(100).then(() => {
    editor.submit();
    console.log("Main 3");
});

which then generated the expected sequence and the database was updated.

Main 1
Main 2
- Sub 1
- Sub 2
Main 3

However, this is a bit of clunky hack; surely .submit() should wait for any dependent triggers to complete before processing?
Or is there an alternative way to proceed (other than moving all my dependent logic to be server-side ... which I'd really rather avoid if I can).

Thanks

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Answer ✓

    Agreed - this is something Editor should do internally. I've made a note of that for a future release. At the moment what you would need to do is set a flag for each dependent field to indicate if it is processing or not. If not, then the submit can complete. If it is, then wait a moment and try again:

    var dependentCounter = 0;
    
    editor.dependent( 'fieldName', function () {
      dependentCounter++;
    
      ... do dependent processing and on complete / success
      dependentCounter--;
    } );
    
    editor.on( 'preSubmit', function () {
      if ( dependentCounter !== 0 ) {
        // Cancel this submit, but try again shortly
        setTimeout( function () {
          editor.submit();
        }, 500 );
        return false;
      }
      // otherwise, all good, let it run
    } );
    

    Regards,
    Allan

This discussion has been closed.