Google Ajax Libraries API handler for CakePHP


/ Published in: PHP
Save to your folder(s)

[Google's "AJAX Libraries API"](http://code.google.com/apis/ajaxlibs/) is great and all, but can slow things down if you're doing a lot of refreshing/cache clearing while debugging. Here's a neat trick: stuff something like this in the `` of your CakePHP Layout to use local versions of these libraries while you're debugging:


Copy this code and paste it in your HTML
  1. /**
  2.  * Google Ajax Libraries API handler for CakePHP
  3.  *
  4.  * Neat trick to load local versions of JavaScript libraries while
  5.  * debugging, but Google-hosted versions for production.
  6.  * Automatically detects whether debugging is turned on, and
  7.  * defaults to jQuery, the best JavaScript library.
  8.  *
  9.  * Just shove it in your <head> and forget about it :)
  10.  * I usually store it somewhere like: /elements/google-jsapi.ctp
  11.  */
  12.  
  13. if (!isset($library)) $library = 'jquery';
  14. if (!isset($useLocal)) $useLocal = (Configure::read('debug') != 0);
  15.  
  16. if (!$useLocal) e($javascript->link('http://www.google.com/jsapi'));
  17. else $library .= '-local';
  18.  
  19. switch ($library) {
  20.  
  21. case 'jquery':
  22. e($javascript->link('google-jsapi-jquery'));
  23. break;
  24. case 'jquery-local':
  25. e($javascript->link('jquery'));
  26. e($javascript->link('jquery-ui'));
  27. break;
  28.  
  29. case 'prototype':
  30. e($javascript->link('google-jsapi-prototype'));
  31. break;
  32. case 'prototype-local':
  33. e($javascript->link('prototype'));
  34. e($javascript->link('scriptaculous'));
  35. break;
  36.  
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.