Posted By

jdbartlett on 06/05/09


Tagged

cakephp


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

rossjha
widgetyegg


Google Ajax Libraries API handler for CakePHP


 / Published in: PHP
 

Google's "AJAX Libraries API" 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 <head> of your CakePHP Layout to use local versions of these libraries while you're debugging:

  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  

You need to login to post a comment.