How to load js files in ajax_view out the elgg_view_page?

Guys. I registered an ajax_view and inside it I have put an elgg_load_js which is duly registered. The problem is that elgg_load_js function only runs on elgg_view_page function which is the last function in the process to show the entire page. I only need the html generated by the elgg_view_entity function and along this some js files.

Does anyone know if it is possible via some function of Elgg?

Thank's

  • Elgg core does not contain any mechanism for dynamic resources loading in out-of-the-box version. There are plans of integrationg in future AMD AFAIR. For now you'll need to make up some mechanism on your own or search for plugin doing it (I think it should be possible to wrap such feature in plugin).

    Second concern is if JS will actually run after document ready event has passed, or requires specific loading order. You need to handle such situations yourself regardless of mechanism.

    Maybe there's some plugin doing that stuff already?

  • Have you tried the getScript() API in your ajax call?

  • My solution was create a view and include it if is a ajax request (detected with elgg_is_xhr function).

    <?php
    $is_ajax = elgg_extract('is_ajax', $vars, FALSE);

    if ( !$is_ajax ) {
        return TRUE;
    }

    global $CONFIG;
    $itens = array('veeplay', 'swfobject');
    ?>

    <script type="text/javascript">
    $(function(){
       <?php foreach ($itens as $name) {
           $item = elgg_extract($name, $CONFIG->externals_map['js'], FALSE);
           if ( $item and $item->url ) {
             echo sprintf('$.getScript("%s");', $item->url);
          }
       } ?>
    });
    </script>