infinite process running during dependent call

infinite process running during dependent call

cpshartcpshart Posts: 246Questions: 49Answers: 5

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Hi, I am experiencing a couple of issues when using the editor and the dependent function to update a field value which I believe may be related, my console is reporting an error Unchecked runtime.lastError: The message port closed before a response was received

The line 844 shown in the attached screen shot is running in an infinite loop

The section of client code is shown below which updates a total editor field as shown below

    siteEditor.dependent( 'dm_holdings.total', function ( val, data ) {
        var stock_id = Number(siteEditor.field('dm_holdings.stock_id').val());
        var quantity = Number(siteEditor.field('dm_holdings.quantity').val());
        var price = Number(siteEditor.field('dm_holdings.price').val());
        var total;
        total = price * quantity / 100;                     
        
        console.log('line 844, siteEditor.dependent dm_holdings.total / total:',total);
        
        siteEditor.set('dm_holdings.total', (total));   
    });

The system adds a row successfully to the table, but simply leaves this process running indefinitely in the background.

I can provide access details to my system by PM, otherwise you may have received them already, with thanks.

Client File to Edit
https://www.dividendview.co.uk/wp-admin/post.php?post=28516&action=edit

Run Client File Page
https://www.dividendview.co.uk/stock-holdings/

Any help much appreciated.

Best regards

Colin

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    Can you try calling the callback, like in this example here, please. Without it, you may see what you've described,

    Colin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Colin

    I have added the callback as above and I am no longer getting the error message Unchecked runtime.lastError: The message port closed before a response was received, so thats good news.

    The line 844, siteEditor.dependent dm_holdings.total process however is still running endlessly, until the page is refreshed.

    I am getting the same problem on a number of my scripts, as they all use the same dependent functionality.

    any other thoughts ?

    Thanks

    Colin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Colin

    Did you have any suggestions on a fix for this problem of the endless process, as it will prevent me from being able to go live in the near future.

    You can access my system as before if that makes it easier to solve.

    Best regards

    Colin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi

    I may have the answer, an alternative to dependent given below, will test when. I get home

    https://editor.datatables.net/reference/api/field().input()

    I will update post if it works

    Regards

    Colin

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

    I think I understand - rather than the process hanging, the function is being called infinitely.

    That is because you are setting the value of dm_holdings.total inside a dependent function:

    siteEditor.set('dm_holdings.total', (total));

    So yes, the value change would then trigger the dependent update, and thus cause an infinite loop.

    Do you not want to have the total field dependent on the three other fields?

    Allan

  • cpshartcpshart Posts: 246Questions: 49Answers: 5
    edited November 2020

    Hi Allan

    Thanks for getting back, you are absolutely correct my silly mistake..

    the lines should be
    siteEditor.dependent( 'dm_holdings.quantity', function ( val, data, callback ) { ... etc. total = price * quantity / 100; siteEditor.set('dm_holdings.total', (total));

    it also works using
    siteEditor.field( 'dm_holdings.quantity' ).input().on( 'change', function () {

    My only problem is I would like the total to update as you type the quantity into the field, whereas it only updates after moving to another field or creating the record.

    so say a price of 150 in pence, set after stock selection

    quantity 1000, should
    display
    total 1.5 (in GBP) when quantity 1
    total 15 when quantity 10
    ..etc.
    total 1500 when quantity 1000

    The stock selection updates price on selection using the dependent function, but is this possible in this instance when typing a number into a text field.

    I am also still getting this error

    Unchecked runtime.lastError: The message port closed before a response was received.

    after adding callback(true);

    It does not appear to adversely impact on functionality, but I maybe wrong.

    Thanks

    Colin

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

    My only problem is I would like the total to update as you type the quantity into the field

    You can tell dependent() to use an event other than it's default of input - e.g. you could use keyup. See the final example on the dependent() documentation for how to do that.

    after adding callback(true);

    Should be callback({}) I think.

    Allan

  • cpshartcpshart Posts: 246Questions: 49Answers: 5
    edited November 2020

    Hi Allan

    Many thanks, I think it will make a big difference if the calculation is done whilst typing.

    I will give your suggestion a try tomorrow.

    Best regards Colin

  • cpshartcpshart Posts: 246Questions: 49Answers: 5

    Hi Allan

    All working by adding to end of dependent event as follows

    siteEditor.dependent( ['dm_holdings.quantity', 'dm_holdings.price'], function ( val, data, callback ) {

    and at the end

    }, {event: 'change keyup'} )

    Many thanks

    Colin

This discussion has been closed.