Source on GitHub
Definition: shared/definitions/load-definition-variables-data.json
shared/client/load-definition-variables-data.js · sjs:load-definition-variables-data.client
View on GitHub
import { Model } from "survey-core";
async function createSurvey(recordId) {
if (window.FORM_DEFINITION) {
// Option A — your server rendered the definition into the page
return new Model(window.FORM_DEFINITION);
}
// Option B — one request returns the definition, variables and previous answers
const { definition, variables, data } =
await fetch(`/api/forms/claim-request?record=${recordId}`).then(r => r.json());
const survey = new Model(definition);
survey.setVariables(variables); // all at once; read as {user.name}, {plan}… and never saved with the response
survey.data = data; // previous answers: edit instead of starting blank
return survey;
}
routes/examples/load-definition-variables-data.php · sjs:load-definition-variables-data.server
View on GitHub
// Option A — render the definition into the page: no database, no extra request
Route::get('/claim', function () {
$definition = json_decode(file_get_contents(base_path('shared/definitions/load-definition-variables-data.json')));
$definition->title = 'Hi '.Str::before(Auth::user()->name ?? 'there', ' '); // change it per request if you like
return view('examples.load-definition-variables-data', ['definition' => $definition]); // Js::from() escapes it for an inline script
});
// Option B — definition, variables and previous answers in one response
Route::get('/api/forms/{id}', function (Request $request, string $id) {
$user = Auth::user();
$definition = DB::table('forms')->where('key', $id)->value('json') ?? abort(404, 'Unknown form');
$data = $request->filled('record') ? DB::table('responses')->where('id', $request->query('record'))->value('data') : null;
return StoredJson::response([
'definition' => json_decode($definition), // a stored document, as stored
'variables' => ['user' => ['name' => $user?->name, 'plan' => $user?->plan]],
'data' => $data === null ? new stdClass : json_decode($data), // previous answers, or {}
]);
});