2

I have a directory tree structure. Each time I click a folder, jQuery.ajax fires and opens a jquery.php file.

This is my javascript code that triggers the jQuery.ajax:

  jQuery('.directory').live('click',function() {

    // Get dir name clicked
    var dir = jQuery(this).find('span').html();

    // Update dir list
    getHTML('getDirList',dir, function(html){
      jQuery('#fileDirList').html(html);
    });

    // Update file list
    getHTML('getRowList',dir, function(html){
      jQuery('#fileList').html(html);
    });        
  });

  function getHTML(instance, dir, callback) {
      jQuery.ajax({
          type: "POST",
          url: "../wp-content/plugins/wp-filebrowser/jquery.php",
          dataType: 'html',
          data: {instance: instance, dir: dir},
          success: function(html){
              callback(html);
          },
          error: function(e) {
            callback('[Error] ' + e);
          }
      });
  }

In this file I have the following code in my jQuery.php file:

<?php

  class jQueryFactory {

  /**
   * Get and output directory list
   */  
  public function getDirList() {
    echo parent::getDirList();
  }


  /**
   * Get images and list them in row format
   */  
  public function getRowList() {
    echo parent::getRowList();
  }


  /**
   * Create new directory
   */  
  function createDir() {
    if(isset($_POST['new_dir'])) {
      $result = parent::createDir($_POST['new_dir']);
      echo $result;
    }
  }


  /**
   * Delete file
   */ 
  function deleteFile() {
    if(isset($_POST['file'])) {
      $file = $_POST['file'];
      parent::deleteImage($file);
    }
  }
}

// Does this work?
if(!isset($factory))
  $factory = new jQueryFactory();

switch($_POST['instance']) {
  case 'deleteImage'    : $factory->deleteFile(); break;
  case 'createDir'      : $factory->createDir(); break;
  case 'getDirList'     : $factory->getDirList($dir); break;
  case 'getRowList'     : $factory->getRowList($dir); break;
}   
?>

My question is: Do I have to fire this function each time I click? Or can I fire once and then just call the various functions within the same user session?

Steven
  • 19,224
  • 47
  • 152
  • 257
  • Not sure what you mean. What does `jQueryFactory` do? Do you mean persisting variables in PHP across multiple Ajax calls? – Pekka Oct 08 '11 at 12:07
  • @Pekka - I want to persist `jQueryFactory` when I navigate the tree structure. But I'm not sure if that's possible? – Steven Oct 08 '11 at 12:15
  • 1
    As the object dies with the end of the script execution, no, what you're looking for is not possible. – rodneyrehm Oct 08 '11 at 12:18
  • @rodneyrehm, that's what I was wondering about. Thanks :) – Steven Oct 08 '11 at 12:24

2 Answers2

1

Every Ajax request you make is going to result in a new request on server side.

It is normal in PHP to initialize classes and configuration variables on every request, so the way you show is fine and trying to persist the jQueryFactory object across requests is not an idea worth pursuing. It shouldn't be much of a performance problem either.

If the process really needs speeding up, look into things like opcode caching.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

can you store $factory in a $_SESSION variable?

maybe something like this? (untested, and i'm not familiar with jQueryFactory)

session_start();
$factory = isset($_SESSION["factory"]) ? $_SESSION["factory"] : makeNewFactory();

function makeNewFactory() {
  $fact = new jQueryFactory();
  $_SESSION["factory"] = $fact;
  return $fact;
}
rlemon
  • 17,518
  • 14
  • 92
  • 123
  • `jQueryFactory` extends `FileBrowser` which has a variable containing root dir path. I would think exposing that wouldbe a security vulnerability. – Steven Oct 08 '11 at 12:32
  • @Steven storing stuff in a session (unlike in a cookie) doesn't expose anything to the client, so that wouldn't be a problem. But this probably won't make sense either way - you have to load the class bluepring for `jQueryFactory` anyway on every request, so the advantage of storing the object in a session variable will bring no benefit. – Pekka Oct 08 '11 at 12:38