<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Karl Stanton &#187; Javascript</title>
	<atom:link href="http://karlstanton.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://karlstanton.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 23 Jun 2010 03:51:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Custom AJAX Wrapper for jQuery</title>
		<link>http://karlstanton.com/2009/10/custom-ajax-wrapper-for-jquery/</link>
		<comments>http://karlstanton.com/2009/10/custom-ajax-wrapper-for-jquery/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 18:13:14 +0000</pubDate>
		<dc:creator>karl</dc:creator>
				<category><![CDATA[Geekisms]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://karlstanton.com/?p=393</guid>
		<description><![CDATA[I write a lot of my Javascript with the help of jQuery, and one of it&#8217;s finest features is it&#8217;s extensibility and powerful internal functions. For larger sites, especially portals, AJAX can become messy if it&#8217;s not controlled, and when things get out of hand, it&#8217;s often hard to trawl through your scripts to make [...]]]></description>
			<content:encoded><![CDATA[<p>I write a lot of my Javascript with the help of jQuery, and one of it&#8217;s finest features is it&#8217;s extensibility and powerful internal functions. For larger sites, especially portals, AJAX can become messy if it&#8217;s not controlled, and when things get out of hand, it&#8217;s often hard to trawl through your scripts to make changes.</p>
<p>That&#8217;s one of the reasons why I wrote (and use almost daily) my custom AJAX wrapper for jQuery.</p>
<p><strong>The idea.</strong><br />
Centralize one $.ajax() method, and have methods external to jQuery control the data and callback methods before firing.</p>
<p>This allows for a global wrapper on all before and callback methods in case you need to do things like: show loading animations, run site statistics, setup error handling, etc.</p>
<p><strong>The code.</strong><br />
I like to use namespaces in my projects, so I will be giving you a basic outline of a namespace setup, and then how an external function can interact with the global AJAX method. </p>
<p>I will base the example loosely on a CRUD schema.</p>
<pre class="js">
// Create our namespace
var Website = {};

/**
 * Outline our webservices
 * @param {String} action - AJAX action to perform
 * @param {Object} payload - Javascript object containing AJAX method properties
 */
Website.Webservice = function (action, payload) {

    switch (action){

    case 'create':
        payload.type = 'POST';
        payload.url = '/ajax/create_data.php';
        break;

    case 'read':
        payload.type = 'GET';
        payload.url = '/ajax/get_data.php';
        break;

    case 'update':
        payload.type = 'POST';
        payload.url = '/ajax/update_data.php';
        break;

    case 'delete':
        payload.type = 'POST';
        payload.url = '/ajax/delete_data.php';
        break;
    }

    // Call the global AJAX method
    Website.AJAX(payload);

}

/**
 * Fires off the AJAX object with user defined payload information.
 * @param {Object} payload	- AJAX data options to bind to the jQuery object
 */
Website.WebService.AJAX = function (payload) {

    // If dataType wasn't specified in the payload, default to 'html'
    var dataType = (payload.dataType !== undefined) ? payload.dataType : 'html';

    // jQuery AJAX object
    $.ajax({

        // Normal properties
        type: payload.type,
        url: payload.url,
        data: payload.data,
        dataType: dataType,

        // Global beforeSend wrapper with user defined function
        beforeSend: function () {

            // Do global here
            if (typeof payload.beforeSend === 'function'){
                payload.beforeSend();
            }

        },

        // Global success wrapper with user defined function
        success: function (data) {

            // Do global here
            if (typeof payload.success === 'function') {
                payload.success(data);
            }

        }
    });

};
</pre>
<p>Now that I have set up our basic framework, I can fire off any AJAX request with minimal code, and abstract any intense data mining functions and other methods from our AJAX process. This allows for clean and controllable code.</p>
<pre class="js">
/**
 * Some function or event that fires off AJAX
 * @param {Object} data - Javascript object containing user data
 */
var updateUserData = function (data) {

    // Create the AJAX property object
    var bundle = {

        data: {
            'user_id': data.user_id,
            'user_email': data.user_email,
            'user_firstname': data.user_firstname,
            'user_lastname': data.user_lastname
        },

        beforeSend: showSpinningLoader,

        // Success callback function
        success: onRefreshSuccess

    };

    // Fire the bundle off
    Website.WebService('update', bundle);

};
</pre>
<p>And there you have it. Simple and easy to use. Now you can trigger AJAX from anywhere in your application with all processes centralized.</p>
]]></content:encoded>
			<wfw:commentRss>http://karlstanton.com/2009/10/custom-ajax-wrapper-for-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>I will now sum up my week with 1 animated gif.</title>
		<link>http://karlstanton.com/2008/12/i-will-now-sum-up-my-week-with-1-animated-gif/</link>
		<comments>http://karlstanton.com/2008/12/i-will-now-sum-up-my-week-with-1-animated-gif/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 14:06:30 +0000</pubDate>
		<dc:creator>karl</dc:creator>
				<category><![CDATA[Geekisms]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[The Duke]]></category>

		<guid isPermaLink="false">http://karlstanton.com/?p=168</guid>
		<description><![CDATA[See you on the other side.]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" title="The Duke" src="http://s379.photobucket.com/albums/oo238/djlynt/th_duke.gif" alt="" width="55" height="68" /></p>
<p>See you on the other side.</p>
]]></content:encoded>
			<wfw:commentRss>http://karlstanton.com/2008/12/i-will-now-sum-up-my-week-with-1-animated-gif/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
