Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Matthew

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

OnlyOneOfClass: Extension to prototype library allowing AJAX calls to supercede / abort / cancel previous calls within a given class.

   1  
   2  // Extension to Ajax allowing for classes of requests of which only one (the latest) is ever active at a time
   3  // - stops queues of now-redundant requests building up / allows you to supercede one request with another easily.
   4  
   5  // just pass in onlyLatestOfClass: 'classname' in the options of the request
   6  
   7  Ajax.currentRequests = {};
   8  
   9  Ajax.Responders.register({
  10  	onCreate: function(request) {
  11  		if (request.options.onlyLatestOfClass && Ajax.currentRequests[request.options.onlyLatestOfClass]) {
  12  			// if a request of this class is already in progress, attempt to abort it before launching this new request
  13  			try { Ajax.currentRequests[request.options.onlyLatestOfClass].transport.abort(); } catch(e) {}
  14  		}
  15  		// keep note of this request object so we can cancel it if superceded
  16  		Ajax.currentRequests[request.options.onlyLatestOfClass] = request;
  17  	},
  18  	onComplete: function(request) {
  19  		if (request.options.onlyLatestOfClass) {
  20  			// remove the request from our cache once completed so it can be garbage collected
  21  			Ajax.currentRequests[request.options.onlyLatestOfClass] = null;
  22  		}
  23  	}
  24  });
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS