Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / Java

Ajaxion - Standalone AJAX - Part 2 of 2 - C# and Java Example

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
22 Jan 2013CPOL5 min read 55.3K   1.4K   42  
An article about how to keep AJAX simple as it is and get the most out of it.
/// Page specific events handlers and eventParams //////////////////////////////////////////////////////////////////////

var textTest = "textTest";
var xmlTest = "xmlTest";
var xmlTestClear = "xmlTestClear";
var showLevel = "showLevel";
var resetLevel = "resetLevel";
var imageUrlIe6 = "imageUrlIe6";
var imageUrl = "imageUrl";
var imageBina = "imageBina";
var imageBina2 = "imageBina2";

var ajaxion = new Ajaxion("");

// Override ajaxion.parseEventParam for this page
ajaxion.parseEventParam = parseEventParam;
function parseEventParam(eventId)
{
	var eventParams;

	switch (eventId)
	{
		case textTest :
			ajaxion.beginEvent(eventId);
			eventParams = window.document.getElementById("text").value;
			break;

		case xmlTest :
			ajaxion.beginEvent(eventId);
			eventParams = GetXmlTestParam();
			break;

		case xmlTestClear :
			ajaxion.beginEvent(eventId);
			eventParams = null;
			break;

		case showLevel :
			eventParams = InitShowLevelEvent();
			break;

		case resetLevel :
			ajaxion.beginEvent(eventId);
			eventParams = null;
			break;

		case imageUrlIe6 :
			ajaxion.beginEvent(eventId);
			ajaxion.setEventMonior(eventId, imageUrl + "," + imageBina);
			eventParams = document.Form1.dropDown.options(document.Form1.dropDown.selectedIndex).value;
			break;

		case imageUrl :
			ajaxion.beginEvent(eventId);
			ajaxion.setEventMonior(eventId, imageBina);
			eventParams = window.document.getElementById("dropDown").value;
			break;

		case imageBina :
			ajaxion.beginEvent(eventId);
			eventParams = window.document.getElementById("dropDown").value;
			break;

		case imageBina2 :
			ajaxion.beginEvent(eventId);
			eventParams = window.document.getElementById("dropDown").value;
			break;

		default :
			eventParams = null;
			alert("Unexpected ajaxion event id: " + eventId);
			break;
	}

	return eventParams;
}

// Override ajaxion.endEvent and ajaxion.endEvent for this page
ajaxion.beginEvent = beginEvent;
ajaxion.endEvent = endEvent;

function beginEvent(eventId)
{
	ajaxion.showStatus("status", "Processing...", "coral");
	ajaxion.showStatusImg("statusImg", "images/processing.gif");
	ajaxion.setEventMonior(eventId, null);
}
function endEvent()
{
	ajaxion.setEventMonior(null, null);
	ajaxion.showStatus("status", "Ready", "silver");
	ajaxion.showStatusImg("statusImg", "images/ready.jpg");
}

/// Event callback handlers //////////////////////////////////////////////////////////////////////

// textTest

function GetTextCallback()
{
	if (ajaxion.isEventConsumed() && ajaxion.getRequest().status == 200)
	{
		window.document.getElementById("result").value = ajaxion.getResponseString();
		ajaxion.endEvent();
	}
}

// xmlTest

function GetXmlTestParam()
{
	 return "<row><head>Row:</head><value>1</value><value>2</value><value>3</value>"
		  + window.document.getElementById("text").value + "</row>";
}
function GetXmlHtmlCallback()
{
	try
	{
		if (ajaxion.isEventConsumed())
		{
			var html;
			var xml = ajaxion.getResponseXml();
			if(xml != null && xml.documentElement != null)
			{
				if (xml.documentElement.firstChild.textContent != null)
					html = xml.documentElement.firstChild.textContent;
				else
					html = xml.documentElement.firstChild.text;
			}
			else if (ajaxion.getResponseString() != "")
				html = ajaxion.getResponseString();
			else
				html = "<b>xml doc. was null</b>";
			window.document.getElementById("divHost").innerHTML = html;
			ajaxion.endEvent();
		}
	}
	catch (ex)
	{
		window.document.getElementById("divHost").innerHTML = "<b>Receive data here.</b>";
		ajaxion.endEvent();
		alert("GetXmlHtmlCallback exception: " + ex.description);
	}
}

// showLevel, resetLevel

var poolingLevel = false;
var stopPooling = false;
function InitShowLevelEvent()
{
	if (!poolingLevel && stopPooling)
		stopPooling = false;
	if (!poolingLevel && !stopPooling)
	{
		ajaxion.showStatus("status", "Processing...", "coral");
		ajaxion.showStatusImg("statusImg", "images/processing.gif");
		poolingLevel = true;
		stopPooling = false;
	}
	return 50; // count for 100%
}
function ShowLevelCallback()
{
	try
	{
		if (ajaxion.isEventConsumed())
		{
			if (poolingLevel && !stopPooling && ajaxion.getResponseString() != "DONE")
			{
				window.document.getElementById("level").value = ajaxion.getResponseString();
				ajaxion.setEventMonior(null, resetLevel);
				ajaxion.postUrl("AjaxCallbackWs.asmx/GetLevel", showLevel, ShowLevelCallback);
			}
			else
			{
				poolingLevel = stopPooling = false;
				ajaxion.endEvent();
			}
		}
	}
	catch (ex)
	{
		poolingLevel = stopPooling = false;
		ajaxion.endEvent();
		alert("ShowLevelCallback exception: " + ex.description);
	}
}
function ResetLevelCallback()
{
	if (ajaxion.isEventConsumed())
	{
		window.document.getElementById("level").value = "";
		stopPooling = true;
		poolingLevel = false;
		ajaxion.endEvent();
	}
}

// imageUrl

function GetImageUrlCallback()
{
	try
	{
		if (ajaxion.isEventConsumed())
		{		
			window.document.getElementById("image").src = ajaxion.getResponseString();
			window.document.getElementById("image").title = ajaxion.getParam();
			ajaxion.endEvent();
		}
	}
	catch (ex)
	{
		ajaxion.endEvent();
		alert("GetImageUrlCallback exception: " + ex.description);
	}
}

// imageBina

function GetImageBinaryCallback()
{
	try
	{
		if (ajaxion.isEventConsumed())
		{
			window.document.getElementById("image").src = ajaxion.getResponseString();
			window.document.getElementById("image").title = "";
			ajaxion.endEvent();
		}
	}
	catch (ex)
	{
		ajaxion.endEvent();
		alert("GetImageBinaryCallback exception: " + ex.description);
	}
}

// imageBina2

function GetImageBina2Callback()
{
	try
	{
		if (ajaxion.isEventConsumed())
		{
			window.open(ajaxion.getResponseString());
			ajaxion.endEvent();
		}
	}
	catch (ex)
	{
		ajaxion.endEvent();
		alert("GetImageBina2Callback exception (IE only): " + ex.description);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
New Zealand New Zealand
Coder

Comments and Discussions