0

I have a module, which renders an AJAX form, which passes data to a script via drupal_add_js and stores it in the settings property of the Drupal object. My problem is that I cannot seem to remove old data from the object (a user can toggle options on the form and send new datasets to the javascript). Logically, I thought passing an empty array would clear the values, but it doesn't.

// Clear old data from settings.
$js = array(
  'd3' => array(
    'inventory' => array(
      'chart' => array(
        'dataset' => array(),
      ),
    ),
  ),
);
drupal_add_js($js, 'setting');

I need something like drupal_remove_js($js, 'setting')? or a method for removing object properties?

2 Answers 2

1

I realize this is a very old question, but I had to figure this out recently when using the plotly_js library to make charts.

/**
 * pos_charts_clear
 *
 * The js settings don't get reset as forms are rebuilt
 * Need to remove traces of chart settings between form rebuilds in a
 * heavy handed way. There's no way to do this without dipping into the
 * cache directly.
 *
*/

function pos_charts_clear($key = 'pos_charts') {
  $js = &drupal_static('drupal_add_js');
  foreach ($js['settings']['data'] as $k => $exist) {
    if (key($exist) == $key) {
      unset($js['settings']['data'][$k]);
    }
  }
}
0

drupal_add_js adds javascript to the page <head>, so as far as I know you can't change that anymore with AJAX. Thus placing it in an AJAX callback doesn't work. You need to solve this at Javascript level in the the browser.

You'll have to make sure all data for all options is sent to the client during rendering and then use simple jQuery selectors to enter the correct part of data into the visualizer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.