-
Notifications
You must be signed in to change notification settings - Fork 864
Routing on first page does not work. #9899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello @SamMousa,
In this usage scenario, Page 1 is initially invisible. After the survey model is created from a JSON object, Page 1 doesn't contain any visible elements because of this I hope this clarifies the situation. |
Does this work for all survey modes? Can I set |
I can confirm it does not work for those modes. |
@SamMousa For this modes you have to set Thank you, |
I understand, but this simple and common scenario now is way overcomplicated. What about adding an optional data param to the constructor? That way people can load data and you don't have to do all calculations twice (and on top of that users don't have to manually manage state of the survey ) |
@SamMousa We can make currentElementName property (get/set) it will work with pages in standard mode and with panels/questions otherwise. It will cover all cases. Thank you, |
But i dont know what element i want to set it to. The goal is to load data and then start the survey. All other approaches are workarounds. Why calculate state twice when you can do it only once? |
@SamMousa What do you mean? It has get currentElementName(): string;
set currentElementName(val: string); |
The goal is to load data into the survey. What you're discussing is implementation detail of a bad solution (bad because it doesn't solve the above issues, it's more of a workaround) |
@SamMousa How do you want to solve it? I just don't get it. There is no current page in these modes. Thank you, |
My proposal is to have an optional data param to the survey model constructor. |
@SamMousa Could you please propse the code, because I am likely do not understand the issue you are trying to solve. |
Okay, I'll try in some more detail.
Currently this is the sequence: // Survey is loaded from JSON
const survey = new SurveyModel(json);
// Survey is now in running state, page 1 is not visible.
survey.data = {channel}
// This is the problem spot
// - The survey is already on page 2, even though we want it to be on page 1.
// - All conditions / visibleIf things have been calculated twice, after the constructor and after changing the data.
// - We need to reset the survey to the start, which depends on the running mode (to which you proposed a solution with "currentElementName", which means we have to figure out the mode, and then the first element based on that)
// Render the survey
survey.render('#survey');
My proposal would be to instead run code like this: // Survey and DATA is loaded from JSON
const initialData = {channel}
const survey = new SurveyModel(json, null, initialData); Because the constructor has the data it can load it before the initial calculation of any logic. This makes it more performant and simpler to use for the user. For what its worth, the current best way that I found to move the survey to the beginning is to call Alternatively, if you want provide even more control to the end user with a simpler API to maintain on your side, you could use an initializer callback. const survey = new SurveyModel(json, null, (survey) => {
survey.data = {channe}
}); Example implementation, see this as pseudocode, of course I don't know the code as well as you do, so this might not be the best place to implement it. interface ILoadFromJSONOptions {
validatePropertyValues?: boolean;
initializer?: (survey: SurveyModel) => void;
}
constructor(jsonObj: any = null, renderedElement: any = null, initializer?: (survey: SurveyModel) => void) {
super();
...
this.fromJSON(jsonObj, {initializer});
...
}
public fromJSON(json: any, options?: ILoadFromJSONOptions, initializer): void {
if (!json) return;
this.resetHasLogo();
this.resetPropertyValue("titleIsEmpty");
this.questionHashesClear();
this.jsonErrors = null;
this.sjsVersion = undefined;
const jsonConverter = new JsonObject();
jsonConverter.toObject(json, this, options);
if (jsonConverter.errors.length > 0) {
this.jsonErrors = jsonConverter.errors;
}
initializer(this); // THIS IS THE RELEVANT LINE
this.onStateAndCurrentPageChanged();
this.endLoading();
this.updateState();
if (!!this.sjsVersion && !!settings.version) {
if (Helpers.compareVerions(this.sjsVersion, settings.version) > 0) {
ConsoleWarnings.warn("The version of the survey JSON schema (v"
+ this.sjsVersion + ") is newer than your current Form Library version ("
+ settings.version + "). Please update the Form Library to make sure that all survey features work as expected.");
}
}
}
|
@SamMousa This code makes the perfect sense if you want to increase the performance for a large survey. So you do not run all expressions two times, with empty data and then with the assign data. However, I beleive the following code would lead to the same result: const survey = new SurveyModel();
survey.data = yourData;
survey.fromJSON(json); My idea was a little different. Let's say you want to store your survey progress and you want to restore later the entered data & the current page. Since currentPage (name and no) doesn't make sense for some modes then const survey = new SurveyModel(json);
survey.data = savedData;
survey.currentElementName = savedElementName; //stored in your database |
I like your idea too, but not for this issue. (I created a different issue for it but it was closed) I'll try to find that. Hmm your example, it feels like undocumented behaviour, but if you say that works I'm happy to try that instead of the callback approach! I'll report back tomorrow. |
This works for me. |
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
We have a survey that has routing on the first visible page like this:
We initialize data for this survey like this:
When the survey is rendered we see page2, not page 1.
Steps to reproduce
Expected behavior
I'd expect to be shown the first page, because according to the conditions the question should be visible.
Screenshots
Please complete the following information:
Additional context
Full survey json:
The text was updated successfully, but these errors were encountered: