Hello,
On my application, I record the answers each time a question changes using the onValueChanged
method. I also tell the backend not to lock the questionnaire with a locked
property that I've created and set to false
.
The problem is that if I only put one question in my questionnaire, if I click on the End
button without leaving my input, the onValueChanged
and onComplete
methods are triggered at the same time. Except that in the onComplete
method, I pass locked: true
to the backend.
As a result, since the two requests are sent at the same time, sometimes the locked
property is set to false
in my database.
Could you help me sort this out?
Thanks in advance.
Hello Leo,
When a user clicks Complete/End, the focus is moved away from the input box and the value is submitted; the
survey.onValueChanged
event is fired. Thesurvey.onComplete
event is fired after that. From what I gather, the main issue is that the request which you send from thesurvey.onComplete
function may hit the server earlier than the request which is sent from theonValueChanged
event.I may suggest that you consider sending survey responses within the
survey.onComplete
event before you sendinglock
to a database. However, to avoid sending duplicate responses, you can cache survey responses. Within thesurvey.onComplete
event, do not send the request if the currentsurvey.data
does not differ from the cached data.Please let me know what you think.
Thank you for your reply. However, it still doesn't work. By caching responses and sending responses in the
onComplete
only if they are different from the cached data, I still have 2 requests sending to each other.Hello Leo,
Thank you for the update. I'll discuss possible options with our developers. Please stay tuned.
Hello Leo,
Unfortunately, we can't do much on the client side only. You can handle it on the server side as well.
In SurveyJS Creator we highly suggest to send the change number together with the updated JSON. It is a commonly situation that the server saved change #37 and then change #36 or #35 came. In this case changed #36 and #35 should be ignored.
In you case I would send to the server the change number started with #1 for
onValueChanged
event and send the #100000 onComplete event. It will allow you to ignore all other changed after you save the user response on complete with the change #100000.Thank you,
Andrew
SurveyJS Team
Hello,
Thank you for your reply. However, we already have a system that blocks backend requests if we have
locked: true
in the database, we can't change via the API tolocked: false
. Do you have a solution directly in the frontend to send only one request to the backend?Hello Leo,
You can lock the sending request onValueChanged event by handling survey.onCompleting event. It happens before we clear values invisible questions and do other preparation.
Thank you,
Andrew
SurveyJS Team
Hello,
Thank you for your reply. However, won't
onCompleting
be called afteronValueChanged
?Hello Leo,
The code with
onCompleting
event handles scenarios when the survey model updates it's data based on logic like clearing value for invisible questions (default behavior).If you are talking about the situation when a user was editing text/comment question and then click "Complete" button by mouse then in this case the
onValueChanged
event will be fired beforeonCompleting
event.To handle this situation, I would call the saving on value changed with several hundreds ms delay:
setTimeout(() => { if(!locked) saveTheDataFunc(); }, 300);
instead of direct calling
saveTheDataFunc();
.Thank you,
Andrew
SurveyJS Team