Monthly Archives: April 2013

Jquery ajax always parameters order inconsistent

My single page web application makes several REST style calls to my server.  Jquery $.ajax is the tool of choice for implementing this on the browser-side.  When converting from Jquery version 1.4 to 1.7+ I started to follow a their new coding pattern.  Here’s the old way, the new pattern and a little gotcha (the jquery ajax always parameters are inconsistent depending on success condition) that I write-up here for my reference and potentially the benefit of others.

Old mode of operation using jquery 1.4 $.ajax syntax:

$.ajax({"url":RestURI,
	"type":"GET",
	"dataType":"text",
	success: function(data) {
		// Do success stuff
		// stop logging timer, do logging
		// stop spinnger            
	}, /* end success: */
	
	error: function (req, stat, err) {    
		// Do error stuff        
		// stop logging timer, do logging
		// stop spinner
	} /* end error: */
});

New pattern using jquery 1.7 uses the chainable done(), fail(), and always() Promise methods derived from the  jQuery Deferred object. .See  code snippet below for the general case for how I use it.  It’s pretty straight forward.

// Start spinner
// start logging timer	 
$.ajax({"url":RestURI,
	"type":"GET",
	"dataType":"text"})
	.done( function(data, textStatus, jqXHR) {
		// Do success stuff
	}) /* end done: */
	
	.fail( function (jqXHR, textStatus, err) {
		// Do error stuff    
	}) /* end fail */
	
	.always( function() {
		// stop logging timer, do logging
		// stop spinner
	}); /* End always */

The new $.ajax pattern lets me have multiple always, done and fails; so I thought it would be good to add a .always at the top — something I would run first before a done or fail. Well, I discovered that the ajax always parameters are inconsistent; that is, the always function returns two different sets of parameters depending on how the $.ajax function returned. If the ajax function failed, then the always callback parameters were (jqXHR, textStatus, err ); if the ajax function was successful, the always callback parameters were flipped: (err, textStatus, jqXHR ).

So if you want to use the always function, then I recommend you setup a callback with no parameters or you do a test of the textStatus parameter — “success” This quirk got me a couple of times. I found references to it on the jQuery bug list — this behavior is by design. The code snippet below captures the issue.  This is not a show stopper; the new promise paradigm is pretty cool and I have some cases where I am chaining together multiple ajax calls, where this will come in really handy.

// Start spinner
// start logging timer	 
$.ajax({"url":RestURI,
	"type":"GET",
	"dataType":"text"})
	.always( function(arg1, textStatus, arg3 ) {
		// on fail: always( function(jqXHR, textStatus, err ) { and textStatus != "success"
		// on done: always( function(err, textStatus, jqXHR ) { and textStatus == "success"
		console.log("First Always function. textStatus="+textStatus+"\n");
	}) /* end always (top) */
	
	.done( function(data, textStatus, jqXHR) {
		// Do success stuff
	}) /* end done: */
	
	.fail( function (jqXHR, textStatus, err) {
		// Do error stuff    
	}) /* end fail */
	
	.always( function() {
		// stop logging timer, do logging
		// stop spinner
	}); /* End always */
Engineering Fountain

Purdue Campus Walk

Here’s the background behind my Purdue Campus Walk:  I took the day off of work and drove down to West Lafayette to attend the celebration of the Purdue’s Computer Science Department’s 50th anniversary. This was a good chance for me to meet former professors, fellow alumni, students, and listen to day of distinguished speakers.

Memorial GymBefore this day of activities, I went for an early morning walk through the center of campus.  My meetings started at 10am, and I timed my walk with the rise of the early morning Sun over the tree tops — making for some very nice lighting on the buildings.

I started my Purdue Campus Walk at the Purdue Memorial Union, walked West along State Street, cross through Memorial Mall, crossed by the Armory to University Street, then headed back through the Engineering Mall, then cut through the Chemistry Buildings back to the Union.

Along the way, I saw:  Purdue Memorial Union, St. Thomas Aquinas, University Hall, Spirit Arch, Purdue Bell Tower, Elliott Hall of Music, Memorial Gym, Math Sciences Building, Lawson Computer Science Building, Hovde Hall, Engineering Fountain, Potter Building, and the Wetherill Laboratory of Chemistry.

This is a favorite campus walk for me.  Nice familiar place, with lots of good memories.  This morning was very nice morning for a Purdue Campus Walk.

Lawson Building

Tips:


If you can, get a room at the Union Club Hotel.  This is the only hotel on campus.  Since the campus doesn’t permit cars, it is very hard to find a parking place.

At the Union, you can park in the hotel garage and walk anywhere on campus.  Everything is nice and close — easy walking.

Purdue Campus Walk – Union, University Hall, Bell Tower, Engineering Mall at EveryTrail

Vint Cerf at Purdue

Vint Cerf Presenting at PurdueBackground on my Vint Cerf at Purdue posting:  I attended the celebration of the 50th anniversary of the founding of the Computer Science department at Purdue University. It was an excellent chance for me to see former professors, alumni and attend a day of presentations by distinguished lecturers, including Vint Cerf, on of the founders of the Internet.

Vint Cerf at Purdue on “Political, Economic, Social and Technical (PEST) Impacts of the Internet”

Purdue UniversityHe told the story of how the Internet was founded. He very modestly discribed his role and shared a bunch of factoids on the growth of the Internet and some new initatives.

Some topics that caught my attention: IPv6, internationalization (UTF-8), new gTLD, DNSSec, Sensor Networks, Smart Grid, Mobile Devices, poisoned caches, routing system hijacking, a laptop integrated into a surf board (surfing while surfing), digital vellum, Openflow and content based routing, MOOC, etc.

Interplanetary Internet

[youtube  http://www.youtube.com/watch?v=XTmYm3gMYOQ&w=300&h=180]

He was particularly excited telling his story behind interplanetary internet. He described the problem of transmitting a packet to Mars where the propagation distance can vary between 7-40 minutes of round trip delay, or even worse given that the planet rotates out of coverage once per Mars day. He described that space craft in the future will be designed to perform deep space network relaying for the benefit of follow on missions.

CDsHe was particularly stuck by a meeting with a librarian who showed him a perfectly preserved manuscript from over a 1000 years ago. He was challenged to show them any computer-based storage technology that can hold its meaning for a 1000 years (let alone 10)! He said that Internet needs to create a Digital Vellum.

Do Anything Differently?

A member of the audience asked what was wrong with the original Internet design and what he would do differently? The question was received with some laughter because it implied that Internet designer had done a less than perfect job, but in good spirit he answered: IP address space and security.

For IP addresses, they discussed number of countries, number of sites, and numbers of servers, based on creating something that mirrored the DARPA net. 32 bits was more than enough. They debated variable vs fixed length, and they decided that since the Internet was a research project that they could revisit the decision if they ever decided to productize the design.

For security, they had a whole nicely designed security solution worked-out with the military. It never got put into the Internet because noone had sufficient security clearance to know what the security architecture was going to be. Further, the original paper by Martin Hellmen, et al (Diffie-Hellmen Key Exchange) didn’t come out until the late 70s. If it had been around, they would have used it.

The Machine StopsAnother question from a student in the audience: (paraphrasing) … if the internet is connected to your bathroom scale, your refrigerator and everything in your refrigerator is RFI tagged, then what’s to stop the government from locking your refrigerator if you weigh too much? This question was asked very sincerely. Vint gave a good answer that said this question has been asked since the beginning of the Industrial Revolution around how machines are taking over our lives. He referred the student to a 1909 book called “The Machine Stops,” by E. M. Forester.

Video of Vint Cerf at Purdue

[youtube http://www.youtube.com/watch?v=_-8lXXzQ1e8 ]

Purdue posted a nice introductory video of Vint’s presentation; it’s only the first 3 minutes. At some point, I’d like to get Vint’s ppt slides and maybe a pointer to his full presentation. I’ll post here if I can.

I was honored to sit at his table for dinner. We shared a brief conversation about Google’s Serge LaChapple and his WebRTC project. Vint was very excited about it.

Lawson Building

Lawson Computer Science Building