surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.11k stars 801 forks source link

Option to disable caching on choicesByUrl #3429

Open pelshen opened 2 years ago

pelshen commented 2 years ago

Are you requesting a feature, reporting a bug or asking a question?

Feature

What is the current behavior?

When using choicesByUrl, there is no way to avoid the caching if the URL doesn't change.

What is the expected behavior?

A way to tell surveyJS to always call the endpoint, either as an option so that it happens whenever run() is called internally, or if that is seen as too easy for an unusual use case, just as an option to pass to run() when triggering manually.

How would you reproduce the current behavior (if this is a bug)?

Set up an endpoint that provides dynamic choices depending on current answers to the survey. Use this endpoint in a dropdown question's choicesByUrl property. Even if the endpoint's result would change, it is not called as the endpoint

Specify your

Some more detail on the use case: I want to be able to use previous answers to text questions as options in a dropdown. There is not a way to do this natively, so I have set up an endpoint in my application that provides the list based on the current saved answers (these are updated regularly including between pages and the source question is on a page previous to the question that uses them) and then this endpoint is called by choicesByUrl. The problem I have is that the URL doesn't change as values passed to it don't depend on other questions. In order to get around the caching I've had to add to the url a random parameter each time I call it, which is very inelegant.

andrewtelnov commented 2 years ago

@pelshen Please use the following code: Survey.settings.useCachingForChoicesRestful = false;

Thank you, Andrew

pelshen commented 2 years ago

This doesn't work for me. Looking at the code, this should have the same effect as adding {NOCACHE} to the URL, which I had already tried as well. Maybe this is actually a bug...

The setting/{NOCACHE} in the end only affects this in the onLoad function ChoicesRestful

if (this.isUsingCache) {
      ChoicesRestful.itemsResult[loadingObjHash] = items;
}

but the run function has these lines

if (this.lastObjHash === this.objHash) return;
this.lastObjHash = this.objHash;

so this always short circuits the function if the URL is the same before it calls sendRequest, regardless of isUsingCache

andrewtelnov commented 2 years ago

@pelshen It checks if nothing in this object, then do not run, otherwise it will run many times. We check caching in this line

   if (ChoicesRestful.addSameRequest(this)) return;
   this.sendRequest();

However, in your case, I would make an AJAX call directly to your service. It seems to me you are trying to use SurveyJS as abscraction for AJAX calls in not a common way.

Thank you, Andrew