Make preCreate and preEdit async

Make preCreate and preEdit async

Red SniperRed Sniper Posts: 47Questions: 9Answers: 0
edited March 2023 in Feature requests

Hello there,

I have been wondering, if there would be any issue in making preCreate and preEdit async.
Problem is, I usually have to make some http request to update row data based on what was typed by the user, and before the new row is being added to datatable.

Maybe this feature already exists (for these events or similar ones), in case just let me know where to find it.

Best regards
Davide

Replies

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    Hi Davide,

    There is no option for that at the moment It would probably be possible for me to add a check for a Promise return, but those events are executed on a per row basis, so if you edited 10 rows, you'd have a lot of time to wait for the updates to complete.

    I wonder if a better option would be to use initSubmit, from which you can initially return false while your extra processing is going on, and then at the end of that call submit() from that async function with a flag set so that initSubmit would just be allowed to complete successfully.

    Allan

  • Red SniperRed Sniper Posts: 47Questions: 9Answers: 0
    edited March 2023

    Hi Allan,

    thanks for the quick reply.
    Actually I think it is what I am already doing, but instead in the pre preCreate preEdit .
    When an http req is needed, I activate a flag, and when I get the response I update the data and trigger the submit (all inside the same editor event).

    I was wondering if it currently is the best way to do so, since making the events async would let me write easier to read code. I understand that in such way processing might grow exponentially, but it would be a dev's responsibility to fine tune when or not make an http call, in case.

    For now, I'm pretty happy we came to the same idea. Should you make changes about this, let me know !

    Have a nice day
    Davide

  • Red SniperRed Sniper Posts: 47Questions: 9Answers: 0
    edited March 2023

    Later thoughts: :sweat_smile:
    I have been using preCreate preEdit because I usually need to update a row programmatically. If I should change to initSubmit I must be assured that things will behave the same way (from the docs, it seems (correct me if I am wrong) that that event updates data based on the displayed form, which is not visible if I am updating a row as above, and possibly I cannot trigger the http call in case).

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    The problem with the two pre* events is that they can't be cancelled. The initSubmit however can be.

    Generally speaking I would recommend doing all of the async stuff as part of the submission - once the data is returned from the server (or wherever it is submitted to), Editor assumes that the subsequent code will be synchronous.

    How are you doing the submission of data - could the async lookup you refer to be done as part of the submit handler?

    Allan

  • Red SniperRed Sniper Posts: 47Questions: 9Answers: 0

    I could do that. After further reasonings, I could move my http calls into the initSubmit.

    I have only 1 question left :
    I usually set editor.idSrc as the database ID of a row in datatable. Of course, this column isn't editable from a user, and usually he cannot see the value too.
    Sometimes it happens that I have to ask the server to reserve a new ID from a specific table of the database. If I were using initSubmit , how can I tell editor that the ID I have received should be assigned to the row I am currently creating ?
    The underline data object still doesn't exist, and the column isn't an editable one.

    Should I use the preSubmit instead ?

    Thanks for the help!

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    how can I tell editor that the ID I have received should be assigned to the row I am currently creating ?

    There isn't a way in Editor to do that I'm afraid. The "create" action specifically assumes that there is no id already assigned and that the submission of the form will create it.

    I guess you could create another field in the Editor instance, a hidden one, which you would set to the value of the reserved id and then whatever server-side script you are using can check for that on create and do an update instead of an insert.

    Allan

  • Red SniperRed Sniper Posts: 47Questions: 9Answers: 0

    I understand your point. I would avoid having an editable column containing the id, (it would be too risky for other reasons)..

    If it would make sense from your pov and there could be a way to add the data in the initSubmit params (as in the preCreate or preEdit ), I guess that would be fine too, since I could manually update the data with the id received from the http call.

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    Yeah, editable id is something to be avoided at almost all costs :).

    What sort of data are you looking to add (i.e. for submission, display only, something else)? I'm trying to work out in my head how it all fits together.

    Allan

  • Red SniperRed Sniper Posts: 47Questions: 9Answers: 0

    In the preCreate / preEdit I have access to the data editor reads from the form/row (from the docs, it is the 3rd param : "The data that will be used to update the row / page") . If I could have access to that same data , then I would be able to move all my http calls in the initSubmit.

    Currently, I am using those events because of that: I need to access the data , "standardize" it adding some custom properties , and process those data based on some other criteria. Having that data in the initSubmit would make it possible for me to do what we already said above.

    Let me know
    thanks in advance Allan

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    If I could have access to that same data , then I would be able to move all my http calls in the initSubmit

    That data doesn't exist at the point of initSubmit though. It is the data that is returned from the server that is passed through to the event handler.

    I think you should use ajax as a function, then you are given the data that Editor wants to submit, you can mash it about as you need and return it as needed for the display using the callback. And it is designed to be async, so there will be no issues there.

    Allan

  • Red SniperRed Sniper Posts: 47Questions: 9Answers: 0

    Hi Allan ,
    I am sorry for the late reply.

    I am conducting some tests using ajax. I will let you know if there will issues using it. To now, it seems it may fit our purposes

    Thanks again
    Davide

  • Red SniperRed Sniper Posts: 47Questions: 9Answers: 0

    Hello Allan,

    an update to let you know that Ajax does what I was looking for.
    Also, removing code that has become unnecessary due to this refactor, datatable reacts a little faster than before these changes

    Thanks again!

  • allanallan Posts: 61,697Questions: 1Answers: 10,102 Site admin

    Hi Davide,

    Fantastic. Great to hear.

    Allan

Sign In or Register to comment.