2

I need to add some additional JS data for my views page.

I write in my module:

function mymodule_views_pre_render(&$view) {
if ($view->name == "myview") {

$outdata=array('dataset'=>my_custom_function_which_generates_data());
drupal_add_js(array('mymodule' => $outdata), 'setting');
drupal_add_js(drupal_get_path('module', 'mymodule') . '/js/init.js');   
 }

 }

When the page loads, I can easily access to my data with JS using Drupal.settings.mymodule.dataset object.

(function ($) {

Drupal.behaviors.mymodule = {
    attach: function (context, settings) {

    console.log(Drupal.settings.mymodule.dataset);  //this code executes on page load and on AJAX refresh of the view

    }

    }

    });

But I need to change/refresh data with each view executing when the view refreshes by AJAX. When I use exposed filters and enable AJAX, I expect to get fresh data.

The view content refreshes normally. Also I see in FF console, that my fresh data of Drupal.settings.mymodule.dataset is in JSON.

But variable Drupal.settings.mymodule.dataset stays the same as on the page load when I access it in my code. What are I doing wrong? How to access fresh data of Drupal.settings.mymodule.dataset?

  • Have you looked at your JS aggregation or cache settings to see if they affect refresh results? – longboardnode Apr 13 '15 at 4:57
  • All caches and JS aggregation are disabled. Please note tha I wrote that I see fresh data in JSON response, but my JS can use data that loaded with whole page. If I refresh the page I can get and use fresh data, but I cant use fresh data with AJAX – Sergey Kravchenko Apr 13 '15 at 5:34
  • Is your data present in the local settings object that's passed in to your attach function as the 2nd parameter? Perhaps there was an error that pervented jquery from merging the data into the global Drupal.settings? – dblue Apr 13 '15 at 21:02
  • No, when I try to console.log(settings) I get the same result with console.log(Drupal.settings). I see right data in JSON response only. – Sergey Kravchenko Apr 14 '15 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.