Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.83 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Avoid form multiple submissions and re-enable when done
  2. $(document).ready(function(){
  3.     //preload wait img
  4.     var $waitImg = $('<img />').attr('src', '/img/wait.gif')
  5.     $('.wait').click(function(){
  6.         $(':submit').hide();
  7.         $(this).after($('<span>Processing, please wait...</span>').prepend($waitImg));
  8.     });
  9. });
  10.        
  11. function downloadStarted()
  12. {
  13.   var $def = $.Deferred();
  14.  
  15.   var check = function() {
  16.      $.get('/ajax/check-download-status', function(response) {
  17.         if (response.started)
  18.         {
  19.           $def.resolve();
  20.         }
  21.         else
  22.         {
  23.           //Check again in 3 seconds
  24.           setTimeout(check, 3000);
  25.         }
  26.       }, 'json');
  27.   };
  28.  
  29.   check();
  30.  
  31.   return $def.promise();
  32. }
  33.        
  34. var success = function() { /* reset/hide loading image */ }
  35. $.when(downloadStarted()).then(success);
  36.        
  37. session_start();
  38. $json['started'] = 0;
  39. if ($_SESSION['download_started']) $json['started'] = 1;
  40. return json_encode($json);
  41.        
  42. <form ...>
  43.     <fieldset>
  44.     ...
  45.     <div>
  46.         <input class="wait" id="submit" name="submit" type="submit" value="View" />
  47.         <input class="wait" id="submit" name="submit" type="submit" value="Download as CSV" />
  48.     </div>
  49.     </fieldset>
  50. </form>
  51.        
  52. $(document).ready(function(){
  53.     var cookieCheckTimer;
  54.     var cookieName = 'download_token';
  55.     var $wait = $('.wait');
  56.     var $waitMsg = $('<span>Processing, please wait...</span>').prepend(
  57.         $('<img />').attr('src', '/img/wait.gif').css({
  58.             'vertical-align': 'middle',
  59.             'margin-right': '1em'
  60.         }));
  61.     //add hidden field to forms with .wait submit
  62.     $wait.parent().each(function(){
  63.         $(this).append($('<input />').attr({
  64.             'type': 'hidden',
  65.             'name': cookieName,
  66.             'id': cookieName
  67.         }));
  68.     });
  69.     $wait.click(function(){
  70.         var token = new Date().getTime();
  71.         //set token value
  72.         $('#' + cookieName).val(token);
  73.         //hide submit buttons
  74.         $(':submit').hide();
  75.         //append wait msg
  76.         $(this).after($waitMsg);
  77.  
  78.         cookieCheckTimer = window.setInterval(function () {
  79.             if ($.cookie(cookieName) == token){
  80.                 //clear timer
  81.                 window.clearInterval(cookieCheckTimer);
  82.                 //clear cookie value
  83.                 $.cookie(cookieName, null);
  84.                 //detach wait msg
  85.                 $waitMsg.detach();
  86.                 //show again submit buttons
  87.                 $(':submit').show();
  88.             }
  89.         }, 1000);
  90.     });
  91. });
  92.        
  93. cookieName = 'download_token'
  94. #set file download coockie if asked
  95. if cookieName in request.params:
  96.     response.set_cookie(cookieName,
  97.         #str: see http://groups.google.com/group/pylons-discuss/browse_thread/thread/7d42f3b28bc6f447
  98.         str(request.params.get(self._downloadTokenName)),
  99.         expires=datetime.now() + timedelta(minutes=15))