21
Feb 09

Proper MooTools Ajax Requests

If you read this you are probably aware that I use Ajax (allows a page to load new content without reloading the page) alot. I really like it. It allows me to control my users experience, and doesn’t require flash. It, if done properly, also degrades nicely. Meaning, if the browser does not support it, the user can still proceed to seeing the content. Just not exactly the same way. (often just navigates to the content, and leaves the current page behind).

Because I love it so much, and I see most people using it incorrectly, I want to go over the correct way.

First of all, everything you are doing in MooTools should be between this. The reasoning is that, this waits for the DOM (Document object model… basically the HTML structure) to be completely loaded and ready.

Anything that fires before this would risk not working properly.

window.addEvent('domready', function(){

});

Now we are going to make an instance of the Request class (ajax)

window.addEvent('domready', function(){
	var ajax = new Request();
});

This is actually most often where people go wrong. A lot of people think that this should go inside of a click event listener or something like that… EG

THIS IS WRONG!!!!!!!!!

window.addEvent('domready', function(){

	$( 'elementID' ).addEvent('click', function(e) {
		var ajax = new Request();

	});		

});

The reason it is wrong is because this creates a new Request instance every time elementID is clicked. Basically that is like buying a new car every time you want to go to the grocery store. Not only that, but you dont even get rid of the previous cars. What you want to do is get one car, and drive it where ever you want to go.

So in full what you want to do is something like this

window.addEvent('domready', function(){

	var ajax = new Request({
		url: 'fileToRequest.php';
		onRequest: function(txt){
			$('toLoad').set('html', 'Loading...');
		},	

		onSuccess: function(txt){
			$('toLoad').set('html', txt);
		}

	});	

	$( 'elementID' ).addEvent('click', function(e) {
		ajax.send();
	});		

});	

And… If you want to drive the care somewhere else, you can so this.

		ajax.setOptions({"url": 'NewPlace.php'});
		ajax.send();

 

So make one car, dive it where ever you need.

Note, there are times when you need multi cars, like if you need to pick up info from multi places at the same time, or you need different types of cars.

15
Feb 09

Idea friday – Finish Sunday

Picture 2When the Ideas come, they don’t stop. I work up early on friday to get some work done before I went to my last day of my winter internship at Tellart when I came up with an idea. 

One of my sites, TheGiant.org, is home to a community of over 2500 art collectors. It is just over 3 years old, but friday morning I felt compelled to make it possible to allow the members of the forum to keep track of their Shepard Fairey works. My last semester starts tomorrow, so I knew I had to finish it by tonight. 

I got it done faster than I thought I would, and I think it came out better, and having more functionality than I thought it would when I started it. I managed to interface with the existing database to handle users, messaging etc. The add-on allows users to track their owned, selling, and wanted prints, and even allows for privacy settings for each list. Users can also take advantage of the friends feature the site has and set lists as friends only. Each user has a URL can link to, to allow them to see their collection. The site also allows users to search others wanted/selling/trading lists to help each other find the pieces they want.

The entire system was built in an AJAX fashion which actually made it much faster to code the php.

Mootools allowed me to make the interaction of the site much nicer, and give a lot more feedback to the user when things happen, and the MooTools AJAX allowed me to reduce the entire system down to only 3 pages. The layover system I normally use is called SmoothBox, but for what ever reason wouldn’t work inside of MediaWiki (Where the user adds prints to their collection). I figured I’d give it a shot and make one my self. Knowing some tricks in css (full bowser compatible transparency etc) , I was actually able to do it in 3 lines of JavaScript. It surprised me how well it worked, and SmoothBox never worked on the iPhone, but mine did. Maybe Ill write plugin of my own sometime.


Copyright © 2012 ASM | a blog
Proudly powered by WordPress, Free WordPress Themes, and Search Marketing