Recent Weblogs

Links I like

Ajax and its Alzheimers


The demonstration here is showing that the service retrieves a word by a simple index(number), that word gets returned in english then sent off to three different translators. The retrieval of a new word is considered a new 'state' and thus should be reconstructed when saving in a bookmark or when traversing the browser's history.

Ajax is a great tool to build web widgets and auto completes etc. But when getting into larger scale applications. Where an application takes different states things aren't so easy. An 'Ajax' aka XHR request doesn't trigger any sort of browser history mechanisms. Which is why the whole 'behind the scenes' thing works so well. Lets take a mail application built with ajax, so the page loads and they need to search for a particular email. So they go into the search page, type in a query, submit it, then choose the email from the result. At this point our user has navigated through four essential states.

  1. The initial load, inbox.
  2. The base search page.
  3. The search results page.
  4. The email of choice.

If the user at this point realizes they've chosen the wrong email, clicking back should bring them back to the search results page. But if this mail application took no accomodations for history control then the window either will take them back to their previous page or simply won't allow the user to click the back button.

Back and Forth, History Traversal

What needs to happen is trigger the browser to register a new history index in the window's history without reloading the page. This can be done by using 'internal links'. The problem with this is though it doesn't recreate our javascript manipulated DOM, we have to handle all of that ourselves. Worse yet it doesn't throw an event for this action either. We can catch it when we're the ones adding the parameters to the URL, but we have no idea when a user uses the history traversal controls(back and forth buttons) so unfortunately we have to use a polling technique. Keeping the operations in the polling function to a minimal is ideal. In the AjaxHistory class it only does a simple string comparison, when the strings are different is when it throws the event that the state of the application should change.

Time for Class, a visit to the AgileAjax Library

The three major classes in the library.

  • EventDispatcher, a class to inherit from to do class based events.(Non-DOM events). This is how the two other classes communnicate, avoiding direct method calls, leading to tight coupling. Also due to the nature of asynchronous requests, the event driven communication is much more ideal.
  • AjaxService, the bread and butter of the library, handles all the inconviences of the XHR as well as adding much needed functionality, such as response caching, request timeouts, a much better event system and of course, history control.
  • AjaxHistory, the history component that serves AjaxService, throws an event when the user has accessed the history traversal controls.

Previous work on this problem

  • AjaxService -- the initial attempt at consolidation of XHR commmunications.
  • AjaxHistory -- the first try at managing history controls
  • Ajax bookmarking -- producing stateful applications, such that an ajax derived state can be restored with a URL
  • Agile Ajax -- my first article on the topic of a cohesive single stand alone library for tackling XHR inconsistencies as well as the history problem.
  • Ajax Timeout -- discussing the needs for timeout functionality with an ajax request. Also making the neccessary modifications to the prototype Ajax.Request class to accomodate timeout functionality.

Other References

  • DHTML Nirvana an interesting approach using IFRAMES, it had some shortcomings for me in FF 2.0 but a good effort at the problem.
  • Brad Nuremburg describes a method using window.location, a polling interval and a structured HashMap object.
  • Aaron Swartz talks about the issue, wasn't able to find his implementation or a demo of his approach in action.

The Code

Just to give you an idea of how easy the AgileAjax library makes XHR requests, here is the code I used to implement the demo on this page. Granted I used Prototype for the DOM traversal/manipulation which I do love, but still, enabling history is as easy as that service.enableHistory and now your ajax requests now register with the browser's history module.

google.load("language", "1");

	

window.onload = function(){
	function handleSuccess(xhr){
		
		var obj = eval('(' + xhr.responseText + ')');
		$$('.word').invoke('update', obj.word);
		tService.translate(obj.word, 'en', 'de', Prototype.K);
		tService.translate(obj.word, 'en', 'el', Prototype.K);
		tService.translate(obj.word, 'en', 'hi', Prototype.K);
	}
	function handleTranslation(obj){
		$$('*[lang='+obj.destination+']').invoke('update', obj.translation);
	}
	function handleException(){
		console.log('wtf happen?! %o', arguments);
	}
	var service = new AjaxService('ajax.php');
	var tService = new GoogleTranslatorService();
	
	service.on('success', handleSuccess);
	service.on('exception', handleException);
	
	tService.on('translation', handleTranslation);
	service.enableHistory();
	service.enableCache();
	
	$$('.action').invoke('observe', 'click', function(e){
		service.send({number : Math.round(Math.random() * 10)});		
	});
}

Comments

September 21, 2009mostafa

ko

August 24, 2011Mae23Buckner

If you are willing to buy a house, you would have to receive the <a href="http://bestfinance-blog.com/topics/home-loans">home loans</a>. Moreover, my sister commonly takes a sba loan, which is really fast.

September 16, 2011business loans

I guess that to receive the business loans from creditors you must present a good reason. However, one time I have got a small business loan, because I was willing to buy a house.

October 05, 2011dissertation

Some people do not know where to get the thesis topic about this good topic. Thus, we will recommend your superb information. And some of them will look for the thesis.

October 05, 2011essay writing

this post is very great thing and I guess that I can buy essays or essay writing to know more a bit referring to that from essay writing service.

October 08, 2011mortgage loans

According to my investigation, thousands of persons on our planet receive the business loans from different creditors. Therefore, there is good chances to find a collateral loan in any country.

October 10, 2011top ringtones of 2010

From time to time I do try to search for the knowledge like this, thus always I receive the samsung ringtones and some time the funny ringtones. Thence now I finally have got what I demanded.

October 12, 2011custom essays

I guess that the students want to get know about this good topic and essay writing. At the essay writing services it’s not very hard to order term papers and custom essays about this post.

October 12, 2011buy an essay

I’ve never watched such themes just about this topic. I would choose the essays writers buy essays or essays for sale about that!

October 13, 2011term paper writing

When you utilize the buy custom essays service, you do not waste your cash. Moreover, you make your life easier, ordering customized essays.

October 14, 2011buy term paper

It is payless to have colorful data referring to this good topic. But really oft men turn on the PC and buy term papers and everybody would have the help of the professional online custom writing service to buy custom essay papers from.

October 14, 2011professional research papers

Only, custom term papers writing industry offers such great discounts for academic papers. That's always nice to get a chance to buy a research paper online for good costs.

October 16, 2011buy link

You feel irritated about your traffic? You have not to feel that, just because cheap links service and seo links building issues can save your website quickly. No world crisis will impact on your online business!

November 08, 2011online term paper

Perfectly written customized essay would give people a possibility to get an academic success. Nevertheless, research papers performing could use your free time. Thence that will be workable to buying essay papers to foreclose that.

December 08, 2011home loans

I had got a dream to begin my company, however I did not have got enough amount of cash to do it. Thank goodness my mate said to utilize the business loans. Thence I used the bank loan and realized my desire.

December 18, 2011buy links

Lots people want to increase their traffic. You can do that also when buy text links.

December 21, 2011pay for writing essay

You should feel absolutely protected getting customized academic papers in the web! Famous paper writing company will usually get you guarantees that you receive original essays papers.

December 21, 2011buy term paper

My teacher's requests are not simple to fulfill. Sometimes I order a custom essay and sometimes I accomplish my papers myself.

December 26, 2011plagiarism checker

We are not lawbreakers! Plagiarized issue suppose to be a felony. We always try to illuminate plagiarized stuff. Hence, plagiarism checker helps very much.

December 28, 2011essay writing service

People have a right to choose the essay writing service. There're lots of essays writing organizations online. Therefore, that will be available to choose the most respected corporation among quantities of frauds!

December 28, 2011buy dissertation

Your topic connected with this good topic seems to be fantastic and a lot of scholars can utilize it for their writing thesis. But some of scholars still utilize the assistance of dissertation service.

December 30, 2011site submission service

I get it that seo submission options of seo submission service suppose to be essential for my business! So, my attention is concentrated on optimization!

December 31, 2011essay writing services

No one in the world can assist you with papers writing better than reputable writing company can do. Thus, that's ok if you buy term papers at reputable custom writing corporation.

January 31, 2012mortgage loans

Different people all over the world receive the home loans from various banks, just because that's comfortable and fast.

February 05, 2012resume writers

A lot people pass the responsibility to expert resume writers because they don't have the ability to write a respectable resume in that the argument why customers need to resume writing service, but such people like creator don't do that. Thanks for the information. A kind of interesting article about this topic.

February 05, 2012buy custom essay papers

Buy an essay or custom written essay and be sure you get correct information referring to this good topic*.

February 08, 2012writers job

Web is inundated of the writing jobs service that can provide useful information referring to this post.

February 10, 2012thesis writing

Thanks a lot for such the hottest note more or less about this good topic ! You have to base your own dissertation service, I opine. Because a lot of thesis writing service make things like that and you could create very best dissertation also.

Name
Site
Comment
  CAPTCHA Image
Reload Captcha Image
Captcha