« Sajax 0.12 and the Admin | ^ Main | cfcontent reset="yes" »


Sajax 0.12 and the Admin

I just finished the admin for a client and it turned out SWEET! I wish I could open it up as is for everyone but I can’t. What I am planning on doing is duplicating it, changing graphics, and opening it up to the public for viewing/testing.

So, what makes it so sweet? Sajax! Sajax is nothing but an Ajax engine but it is very simple to implement, after you figure a couple things out. This isn’t my first stab at Ajax stuff. I did a portion of a site in Ajax for a big client about a year ago. It was quite the slick process but Sajax seems to be a bit better here. Let me go ahead and give a brief overview of how to implement Sajax (with PHP).

This is step #1. Simply call the php function to have all of the needed JS included.

<script type="text/javascript">
	<? sajax_show_javascript();?> 
</script>

Step 2 (which could be #1...preference thing here) is nothing but a copy and paste + change 1 line. In the <em>sajax_export()</em> method you simply tell Sajax which methods JS can call. In Step 3 we'll setup our custom methods.
require_once("Sajax.php");
// $sajax_debug_mode = 1;
sajax_init();
sajax_export("helloWorld", "goodbyeWorld");
sajax_handle_client_request();

Step 3 is where you implement your custom methods.
function helloWorld(){
      return "hello world";
}

function goodbyeWorld(){
      return "goodbye world";
}

Note: You can do whatever you want in your custom methods (call a class, web service, read a database, etc) and return it directly to JS. There may be limitations on data types but I'm not aware of any, so far.

This is simple huh? Well, that portion is quite simple. So far we've done normal PHP stuff with a couple extra lines of copy/paste code. Let's move on to Step 4.

Step 4 involves actually seting up your calls to the methods you specified in Step 3.
function doHelloWorld(){
	sajax_request_type = 'POST'; 
	x_helloWorld(helloWorld_Result); 
	sajax_request_type = '';
}

function doGoodbyeWorld(){
	sajax_request_type = 'POST'; 
	x_goodbyeWorld(goodbyeWorld_Result); 
	sajax_request_type = '';
}

Before showing more methods let's look into what is going on here. The first line in each of the functions is sajax_request_type = 'POST';  which specifies the following call as a POST as opposed to a GET. This is simply a preference thing here, for this example. I'm not as well versed as I'd like to say what happens if you leave this line out or not but I know it doesn't hurt to keep it in. :-) (which the admin had to use it anyway)

The next line in each function is x_FunctionName. What Sajax does is any method you setup to export becomes x_FunctionName in Javascript. So, having a method name of johnnyBeGood() becomes x_johnnyBeGood(), if you add it to the export line from Step 2. To pass arguments you would do the following.
x_myFunction(1, 2, 3, 4, 5, 6, 7, 8, "test", "argument 10", resultFunction)

As long as the result function is last you can add any number of params as you’d like. Ok…let’s move on.

Step 5 is actually the final step. We need to specify our result functions. I used the old naming style from Flash Remoting (since Ajax and Remoting are essentially the same).

function helloWorld_Result(result){
     alert(result);
}

function goodbyeWorld_Result(result){
     alert(result);
}

That's it. Everything else you do is purely related to Javascript and how you want the methods to be called (by click, on page load, etc). Now, there are a few nuances I soon learned that I can't leave out.

When you are sending post variables the variables aren't conveniently located where you would like them to be...in the $_POST as a child. Instead they are grandchildren with a parent of $_POST['rsargs']. So, passing variables via post are retrieved like so:
$_POST['rsargs'][0] //retrieves the first post varaiable

At first I based Sajax for doing this but I understand now. Inside of $_POST (if you return it to Ajax then loop over it and display the results you can see this) there are 3 other variables. I don’t recall their names but they hold some other data. At first it was frustrating because I wanted $_POST0 to give me my var. Once I figured that out it was smooth sailing.

That’s about it. There is a lot more in terms of best practices when retrieving remote data, etc that I won’t get into here but I will say keep your visitor in mind when making calls, etc. I hope this helps someone overcome the hurdles I tripped over and rock an Ajax app/site with Sajax.

Disclaimer:
I typed all of the above code off the top of my head, minus the copy/paste code I noted, so it could quite possibly break on you. :-)

Posted by John C. Bland II on March 7, 2006 9:36 PM |

TrackBack

TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/62

Post a comment