PSEC: Simple Data Initialization from Server

SUMMARY: Where I last left off, I was implementing the underlying skeleton of the protocol. I have the bare minimum working. Now it’s time to actually return some real data and draw something on the screen.

There are two files that matter: _ajax.php on the server side, and libs/model.js on the client side. The ajax file is what needs to query the database on behalf of the given command and return useful data.

In the case of the initialize command, the client is expecting a list of tasks and another list of vtasks. The tasks are an array of task objects indexed by task_id. The list of vtasks are similarly an array of vtask objects indexed by task_id as well, except these task_ids represent what’s shown in the task list, in order.

On the PHP side, I’m using the $wpdb->get_results($sql, OBJECT_K) call which returns an array of object with key properties named after the table rows. The array is indexed by the first field returned by the call, which in my case is the id number.

Here’s some really boring code samples:


// read all tasks created with current user_id
$table = $table_prefix . "tasks";
$sql = $wpdb->prepare(
    "
    SELECT id, name, action_id
    FROM $table
    WHERE user_id = ".$arr['user_id']."
    "
);
$results = $wpdb->get_results($sql, OBJECT_K); 
// assign to response array
$arr['tasks'] = $results;

// read all the actions in the system
$table = $table_prefix . "actions";
$sql = $wpdb->prepare(
    "
    SELECT id, action, deliverable, description 
    FROM $table
    "
);
$results = $wpdb->get_results($sql, OBJECT_K); 
// assign to response array
$arr['actions'] = $results;

The JSON packet is eventually parsed by Model.InitializeFromPacket():

InitializeFromPacket: function ( response ) {

    DBG.Out("Model.InitializeFromPacket(): Got Response!");
    
    // grab the data out of the response packet
    Tasks = response['tasks'];
    Actions = response['actions'];

    // test code:
    // iterate through tasks and show related actions
    for (var key in Tasks) {
        var action_id = Tasks[key].action_id;
        var task = Tasks[key];
        DBG.Out("[T"+key + "]:" + task.name + " - [A"+action_id+"]" + "-" + Actions[action_id].deliverable + "-" + Actions[action_id].description);

    // assign data to Model
    Model.Tasks = Tasks;
    Model.Actions = Actions;

}

This produces output that looks like the following:

[JS] [T1]:Make Table - [A1]-Green Table-A 4x6x12 inch table that can be used to make banana bread.  
[JS] [T3]:Make Table 2 - [A1]-Green Table-A 4x6x12 inch table that can be used to make banana bread.

The JSON handling makes data management ridiculously easy. I need to add all kinds of code to ensure robustness of the data transfer.

Next step is to create the GUI part. At minimum, there needs to be an interface for creating new tasks, reordering them, and editing them. After that, I need to write the code that saves changes back to the database. I’m sure there will be all kinds of subtleties to that waiting to trip me up. I’m too tired right now to think about them.