We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17422
    • 225 Posts
    Hi,

    i use a ressource with snippet for load some code via ajax :
    $data=null;
    $menuIndex = $_GET['menuIndex'];
    $criteria = $modx->newQuery('modResource');
    $criteria->where(array('parent'=>"2",'menuindex'=>$menuIndex));
    $resource = $modx->getObject('modResource',$criteria);
    
    $idPage=$resource->get('id');
    $urlPage=$modx->makeUrl($idPage,'','','full');
      //return $urlPage;
      if($http= fopen($urlPage,"r")){
    while(!feof($http)){
    $data .=fgets($http,1024);
    }
    fclose($http);
    }
    preg_match_all('/(<!--DebutContenuArtiste-->)(\W.*)(<!--FinContenuArtiste-->)/imsU',$data,$result);
    foreach($result[2] as $contenu){
    $contenu;
    }
    
    $arr = array ('contenu'=>$contenu);
    return json_encode($arr);​
    



    I find that the page takes a long time to load.

    my question : Is that my way is right?

    if no, how to do for optimize my script

    Thanks for your help

    Laurent


    below, my javascript code tel the ressource
     url=site_url+"/admin/loadajaxartiste.html?menuIndex="+num;
                    $.ajax({
                    dataType:"json",
                    url :url,
                    async: false,
                     beforeSend:function() {
                     },
                    success :function(data){
                       $("#blocArtiste"+num).append(data.contenu);
                        $("#blocArtiste"+num).css(cssDeb);
                        $("#blocArtiste"+num).animate({left:0},900);
                        $("#blocArtiste"+elemCurrent).animate(cssFin,900);
                        tailleBlocBiographie();
                        $(".ppt").remove();
                        $(".pp_overlay").remove();
                        $(".pp_pic_holder").remove();
                        jQuery('.jcarousel-skin-dates').jcarousel({
                            vertical: true,
                            animation: 800,
                          visible:4
                        });
                        jQuery('.jcarousel-skin-discoGalery').jcarousel({
                            animation: 800,
                          visible:3
                        });
                      $("a[rel^='prettyPhoto']").prettyPhoto();
                      JolieScroll();
                    }
    

      • 26016
      • 561 Posts
      Hi,
      I’m not sure about why it’s slow. Your way certainly has merit, but I do it a different way.

      When I do Ajax loads on a page, I wouldn’t use a snippet at all. I load the content just as usual, but use JQuery to show, hide, fade-in, fade-out, slide in, slide out, what-have-you. JQuery can hook stuff to a DIV, a class or ID, all kinds of methods. Definitely not built into MODx, but not too hard to do if you know JQuery. This way, the load is happening on the client side, and the database isn’t even involved, so speed is usually pretty good, and even bad hosting won’t matter much.

      Let’s see what the snippet gurus say. smiley
        MODx and Wordpress development
        Linux, PHP 5.2, MySQL 5.0, Evo 1.05, Revo 2.08-pl, Firefox 4
        • 22303 MODX Staff
        • 10,725 Posts
        AJAX Resources are like any other Resource except you are just returning the specific piece of content or data you need to process with your AJAX handler. I always create my AJAX endpoints as MODX Resources using Snippets.