« Adobe Photoshop CS2 | ^ Main | Sajax 0.12 and the Admin »
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)
Posted by John C. Bland II on March 7, 2006 9:36 PM | Permalink
TrackBack
TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/61



