tags:

views:

1293

answers:

2

I'm developing an application which relies heavily on jQuery for user interaction.
(and if you're browser doesn't support jQuery, then upgrade or don't use my application :)

As normal, one have functions to GET, SET and DELETE data from a table.

In my application, I'm GET'ing and SET'ing a lot of information without page reload. To do this, I mostly use jQuery.post.

A typical code in my JS file looks like this:

jQuery.post("mypath/jquery_getset_data.php", { instance: 'getItems_A', itemID: itemID_value},
  function(data) {
    populateItemList(data);
  });

The jquery_getset_data.php contains of many if statements:

if($_POST['instance'] == 'getItems_A'){
  // PHP code to get and process data from MySQL DB
}

if($_POST['instance'] == 'setItems_A'){
  // PHP code to process and insert data to MySQL DB
}

Here's my question:

  1. Is tehre a better way for interaction between JS file and jquery_getset_data.php?

  2. How can I dynamically call different "remove item" functions inside createStoreList? See update 1.

Update 1: This is the code that I use to create many different lists.

  function createStoreList(data)
  {
    var ul = jQuery("<ul/>");

    // We need to build the html structure in order for this to be registered in DOM.
    // If we don't, jQuery events like .click, .change etc. will not work.      
    for (var i = 0; i < data.length; i++)  
    {
      ul.append(
         jQuery("<li/>")
         .attr("id", "listItem_"+data[i].id)
         .append(jQuery("<span/>")
            .addClass("btnRemoveItem")
            .attr("title", "Remove store from list")
            .attr("id", data[i].id)
            .click(function() { removeItemA(this); })  
          )
         .append(data[i].name + ', ' + data[i].street)
        );              
    }
    return ul;  
  }

Update 2 I figured I just could use switch statements. I've tested it and it works.

    .click(function() {
        switch(instance)
        {
          case 'removeListItemA': removeListItemA(this); break; 
          case 'removeListItemA': removeListItemB(this); break;
          case 'removeListItemA': removeListItemC(this); break;
        }
    })
+1  A: 

The only thing I would change is in jquery_getset_data.php I would use a switch statement instead of many if statements. jQuery's $.post method is fine for what you're doing, talking to a script that affects the database (deletes/updates/etc) using one of the GET ajax methods ($.load or $.get) breaks the HTTP specification.

karim79
Yes, I was thinking of doing that. Turn my pieces of code into functions and call them from the swtich statements.
Steven
"using one of the GET ajax methods ($.load or $.get) breaks the HTTP specification." How do you mean?
Ashley Ward
@Ashley Ward - GET requests are not supposed to yield permanent changes to the application (database updates, insertions, deletions). That's what POST is for.
karim79
+2  A: 

In order to reduce the jquery_getset_data.php I would use the OOP design patterns to avoid switches and if statements.

class ICommand
{
     public:
          function execute( );
};

class CommandGetItemA
{
     public:
           function execute( )
           {
               //do some staff here
           };
};

and then:

CommandsMap['getItemA'] = new CommandGetItemA( );
CommandsMap['setItemA'] = new CommandGetItemB( );
....

CommandsMap[ $_POST['instance']].execute( );

I know looks complicated, but for my taste looks much better. And regarding your second question I'm not sure I understood it, can you add more explanation?

After I saw you update, I think for second question you can do:

.click(function() {
      window[instance]( this);   
});

There the "instance" is the function name, or you can update or append it latter to make it be the function name;

Artem Barger
Each "if" statement execute different code to achieve a goal. How can this OOP pattern reduce the ammount of code? I'm not sure what CommandsMap[] is. Q2 has been updated.
Steven
I'm not claiming it will reduce you the amount of code, but for sure it will made it more readable and clear. Each time you will need add new if statement, here you will need to to write the class with logic you want to add and when "register" that class in the CommandsMap.
Artem Barger