8

Currently I'm using jQuery to load a view, like this:

$.ajax({
    type: 'POST',
    url: Drupal.settings.basePath + 'views/ajax',
    dataType: 'json',
    data: 'view_name=' + viewName + '&view_display_id=default&view_args=' + viewArgument, // Pass a key/value pair.
    success: function(data) {
      var viewHtml = data[1].data;
      target.children().fadeOut(300, function() {
        target.html(viewHtml);

        var newHeightOfTarget = target.children().height();

        target.children().hide();

        target.animate({
          height: newHeightOfTarget
        }, 150);

        target.children().delay(150).fadeIn(300);

        Drupal.attachBehaviors(target);
      });
    },
    error: function(data) {
      target.html('An error occured!');
    }
  });

My problem is, that for example if the view uses the 'Field Slideshow' module, the js/css files needed for it to work properly are not loaded. What could be a nice (= kittens unharmed) solution to this?

5
  • It seems that this will be solved in D8 drupal.org/node/561858. Passing path to js/css file in Drupal.settings.mymodule and getting them with jQuery.getScript() and the jQuery.getCSS() (from github.com/furf/jquery-getCSS) could be a workaround...
    – Temaruk
    Jul 15 '11 at 8:41
  • Apparently the Ajax Framework already lazy loads css/js/library files in D7. Still have to delve deeper, but I feel the solution could be a custom ajax menu callback per view with ajax page delivery...
    – Temaruk
    Aug 4 '11 at 14:39
  • I had a similar issue, still unsolved i though i'd put a link to it here.
    – silkAdmin
    Sep 12 '11 at 22:17
  • I don't know enough to write a full answer, but I think Chaos Tools's AJAX API would already have solved this problem. You might want to use it or check it out. Editable Fields also deals with this issue somehow, so it may be some more reference code to check. Nov 5 '11 at 2:03
  • 1
    @silkAdmin, I posted an answer to your question too. Also it should be noted that in D7 you don't really need to use ctools. The Ajax framework is in D7 Core (api.drupal.org/api/drupal/includes--ajax.inc/group/ajax)
    – ericduran
    Nov 5 '11 at 8:18
7
+50

Hmm, I feel like this question deserves a bigger bounty :-p but none the less here's my attempt at answering this.

Drupal already loads any extra css/js now I can't really find much documentation on it and sadly the JS files aren't easily documented without actually reading through the code but is there.

You really should try to avoid making $.ajax request yourself and instead wrapping everything with Drupal.ajax also if you use the Ajax System you don't even need to write the ajax request your self.

By implementing a proper Drupal ajax request using ajax commands most of the css/js handling will be done for you automatically.

By looking at your above code it looks like you you're making the same request views would normally do it self. So you should probably let views handled the ajax request and overwrite the ajax commands thats going to be fired to be your command. You can do this by either implementing a hook_ajax_comamnd_alter but it kinda sucks with views because you loose all contextual information so I would recommend using hook_views_ajax_data_alter instead.

I know there's not much code in my answer, but it's hard to answer with code, being that the code currently there is not correct to being with.

For more information see Drupal.ajax in the misc/ajax.js file. For an example of implementing an ajax request with Drupal.ajax see the views ajax file. And if you want to see and example of views normal behavior being overwritten by changing the ajax commands see the views_load_more module where I do this behavior.

Also please feel free to comment on this answer, if anything here is unclear.

On and for more info on the Ajax Framework see Ajax Framework for example see the example module, even thought most of what you want to do is not cover in the examples module.

2
  • That must be the right direction, but I would first want to verify. Since asking the question I gained some experience with Drupal 7's Ajax Framework, like creating and using custom ajax commands. I will revisit my problem and see what can be done, and update with info on my findings! Thanks!
    – Temaruk
    Jun 28 '13 at 12:17
  • the url you are refering is not exist: api.drupal.org/api/drupal/includes--ajax.inc/group/ajax
    – shekoufeh
    Feb 11 '17 at 13:31

Your Answer

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

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