1

I have a module that uses a button to load an embed view everytime the button is clicked.

// function to print builder view
    function workout_builder_template() {
        $viewName = 'builder_view';
        print views_embed_view($viewName);
    }

workout_builder_template();

However after the button is pressed a new view is loaded and the view is no long ajaxified. For example, if I click the apply button after changing the exposed forms, the form submits to a new page. Before clicking the button that loads an embed view via ajax, the exposed form altered the view content via ajax.

Here is example html from the ajaxified view

<form accept-charset="UTF-8" id="views-exposed-form-builder-view-default" method="get" action="/drupal7/builder-view" class="ctools-auto-submit-full-form ctools-auto-submit-processed jquery-once-1-processed">

Here is example html from the ajax less view

  <form accept-charset="UTF-8" id="views-exposed-form-builder-view-default" method="get" action="/drupal7/builder-view" class="ctools-auto-submit-full-form">

In the example the ajax less view does not have processed classes added to it. From this I suspect that the new view does not get javascript attached to it after the first ajax event. I have been researching drupal.behaviors and thought about printing

$vars['scripts'] = drupal_get_js();
print $vars['scripts'];

in the workout_builder_template() function displayed above, but I am not sure how this is done

2

I could be wrong here but am going to assume that the fetching of the embedded view and HTML refresh on the client-side has been done using custom Javascript code.

Once new HTML has been added to the DOM in a custom way, then to attach all Drupal behaviors defined by active modules, from within the success callback of your XHR client (say jQuery.ajax()) you might have to manually call:

// $wrapper here is the jQuery object identifying the wrapper element to
// which your new HTML has been injected to. Eg. $wrapper = $('.my-view-wrapper')
Drupal.attachBehaviors($wrapper, Drupal.settings);

My recommendation would be to try and possibly switch to the Drupal AJAX API (Documentation) as it lets you keep this refresh logic on the server-side (one place). The other, obvious benefit is that Drupal.attachBehaviors() is called for you automatically.

  • i don't understand any of that, trying to learn attach behaviors, but got it to work by reloading the scripts after fetching the views embed view, which runs the js again after – Andrae Browne Oct 11 '13 at 23:37

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.