We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33372
    • 1,611 Posts
    This conversation seems to be in danger of becoming an argument instead of a discussion, and I don’t see much benefit in that. Count me as one of the people who think that Ajax loading can definitely improve the responsiveness of some sites, and also as someone who thinks that it can be and often is overused when it adds needless complexity and reduces accessibility. Where one draws that line seems to me to be a matter of opinion, and everyone is entitled to theirs.

    When I do Ajax apps in MODx, I’ve never needed to hack the core and I also don’t use index-ajax.php. I just use regular pages for default content and replace specific divs within them using Ajax calls. Usually the call will load a page that returns a snippet, and the snippet could grab the content of any page or return any other value that might be appropriate.

    So I do have one or more additional pages in my document tree for returning Ajax content, but they just have snippet calls in them and blank templates. You could have one that returns the content of any page when its id is passed as a value, and you wouldn’t need to modify any core code to do that. Not that I think that it’s "wrong" to modify the core code (I’ve done it on occasion myself), but I’m not sure what you get from your solution that can’t be obtained with the existing modular structure.

    Is there something that your solution would do that couldn’t be accomplished using plugins and snippets?
      "Things are not what they appear to be; nor are they otherwise." - Buddha

      "Well, gee, Buddha - that wasn't very helpful..." - ZAP

      Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
      • 30223
      • 1,010 Posts
      I agree with ZAP that there’s no benefit in arguing over the pros and cons and appropriateness of using AJAX calls on web pages. It all depends on your perspective. So what if snop is actually building an application where the use of Ajax is in fact helpful to the user and the solution he is working towards is to provide a convenient fall-back solution for those that do not have javascript enabled? I see nothing wrong with that.

      As far as a solution is concerned I see some merit in the "isXmlHttpRequest" function but instead of hacking the core I think it’s quite possible to do this with a simple plugin using the onWebPageInit event. I played around with it just now and with a plugin and one single extra page for the ajax calls and using a slightly adapted version of the getField snippet I was able to achieve something usable.

      <?php
      //set onWebPageInit system event
      
      $ajax_page  = 108; //id of page handling ajax calls
      
      $e = & $modx->Event;
      
      if(  $e->name=='OnWebPageInit' )
      {
         $id = $modx->documentIdentifier;
         if( isXmlHttpRequest() ){
      		$_SESSION['doc_id'] = $id;
      		$modx->sendForward($ajax_page);
      	}
      }
       
      if(!function_exists('getHeader')){
      	function getHeader($header) {
              if (empty($header)) {
                ;
              }
      
              /* Pokusiť sa ich najpr získať z globálneho poľa $_SERVER. */
              $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
              if (!empty($_SERVER[$temp])) {
                  return $_SERVER[$temp];
              }
      
              /* Toto vyzerá ako jediné riešenie ako získať Authorization header na Apache serveri. */
              if (function_exists('apache_request_headers')) {
                  $headers = apache_request_headers();
                  if (!empty($headers[$header])) {
                      return $headers[$header];
                  }
              }
      
              return false;
          }
      }
      if(!function_exists('isXmlHttpRequest')){
          function isXmlHttpRequest()
          {
              return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
          }
      }
      


      The ajax handling page only has this call in it’s content: I gave it an empty template but you could easily wrap some xml around it if you wanted. The page needs to be un-cached.
      [[getContentField? &field=`content`]]


      Where getContentField is a slightly modified getField snippet. The only change I made is a change to the &docid parameter: (approx. line 24)
      $gfIntDocId = $_SESSION['doc_id']?$_SESSION['doc_id']:($docid  ? $docid : $modx->documentIdentifier);
      


      So if an XmlHttpRequest is made the request is forwarded to the ajax handler page which then uses the getContentField snippet to fetch the original content.
        • 27843
        • 170 Posts
        On my presonal sites i don’t need this, so why i need this ? Because every second client want ajax navigation and I know that every second client doesn’t know what this words represent.

        But i only find the best solution how can i this accomplish and i think that the plugin is the best way.

        I know that my solution isn’t elegant and when i look at core modification i see that it’s only unnecessary fabrication.

        Quote from: ZAP at Feb 23, 2009, 11:13 PM

        So I do have one or more additional pages in my document tree for returning Ajax content, but they just have snippet calls in them and blank templates. You could have one that returns the content of any page when its id is passed as a value, and you wouldn’t need to modify any core code to do that.

        I have tried this too, but i must call $this->parseDocumentSource everytime and i can’t cache the output.

        Quote from: ZAP at Feb 23, 2009, 11:13 PM

        Is there something that your solution would do that couldn’t be accomplished using plugins and snippets?

        No and I think that the best solution will be accomplish this by plugin.
          • 33372
          • 1,611 Posts
          Quote from: snop at Feb 24, 2009, 04:37 AM

          I have tried this too, but i must call $this->parseDocumentSource everytime and i can’t cache the output.
          That’s true, I suppose. I hadn’t thought about displaying parsed content (with TVs, snippet calls, etc.) and wanting to cache it. I’ve never actually needed to do that specifically, so I haven’t experimented with it to see how to cache what I would need for the Ajax call. I can’t see any reason not to cache the page in its non-Ajax context and have a snippet grab the data from its cache file rather than the database if it’s there and just parse out the extraneous HTML.

            "Things are not what they appear to be; nor are they otherwise." - Buddha

            "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

            Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
            • 27843
            • 170 Posts
            I have really good idea, how should the plugin look. If i will have time and desire i create the plugin which will work as i wrote above and will utilize cache too
              • 34017
              • 898 Posts
              I like the idea of a plugin. But the plugin will run on every request- just a little more overhead.

              In the past I’ve created one page with a blank template. And created a snippet to retrieve the page info from the db.

              Then I just made the ajax request with the parameters i needed like

              ajax_request({docId: 8, start: 25})

              It worked well with what I needed.
                Chuck the Trukk
                ProWebscape.com :: Nashville-WebDesign.com
                - - - - - - - -
                What are TV's? Here's some info below.
                http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                • 27843
                • 170 Posts
                Quote from: ChuckTrukk at Feb 24, 2009, 10:48 AM

                I like the idea of a plugin. But the plugin will run on every request- just a little more overhead.

                In the past I’ve created one page with a blank template. And created a snippet to retrieve the page info from the db.

                Then I just made the ajax request with the parameters i needed like

                ajax_request({docId: 8, start: 25})

                It worked well with what I needed.

                This sound good, it’s very similar wink