getDataJs method

String getDataJs()

Generates a script tag with data as a JSON object.

Merges all data from the included JS assets and additional data, then encodes it into a JSON object. This is useful for passing dynamic data to the frontend.

Returns an HTML script tag containing the JSON-encoded data.

Implementation

String getDataJs() {
  try {
    var allData = {};
    for (var asset in js) {
      allData.addAll(asset.data);
    }

    allData.addAll(data);
    var json = jsonEncode(allData);
    return '<script type="text/javascript">;var request = $json;</script>';
  } catch (e) {
    Console.e(e);
  }

  return '';
}