async.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>RequireJS Async plugin</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <style>
  8. .info{background-color:#cfc; border:2px solid #ada; padding:10px 20px; margin:2em 0}
  9. </style>
  10. </head>
  11. <body>
  12. <div id="wrapper">
  13. <h1>RequireJS async plugin</h1>
  14. <div class="info">
  15. <p>
  16. Google Maps loads many JS files asynchronously, so listening just to the first script load
  17. isn't enough to check if it is ready to be used, another problem is that the regular gmaps script
  18. uses document.write, so we need to pass a `callback` parameter to make it not use `document.write`
  19. and wait for the callback call.
  20. <br>
  21. [<a href="http://code.google.com/apis/maps/documentation/javascript/basics.html#Async">More info</a>]
  22. </p>
  23. </div>
  24. <div id="map-canvas" style="width:400px; height:300px; border:1px solid #ccc; background-color:#f5f5f5"></div>
  25. <h2>JSONP</h2>
  26. <div class="info">
  27. <p>
  28. Note that the async! plugin isn't really required for JSONP calls if the response is an <strong>Object</strong>.
  29. If the response is an Array or String you will need the async! plugin. [<a href="http://requirejs.org/docs/api.html#jsonp">reference</a>]
  30. </p>
  31. <p>
  32. The default parameter used to set the callback name is <code>callback</code>, you can set a different name
  33. by passing it at the end of the dependency URL preceded by a exclamation mark (<code>!</code>), e.g.: <code>async!http://example.com/?foo=bar!jsoncallback</code>
  34. </p>
  35. </div>
  36. <h3>Flickr feed</h3>
  37. <div id="flickr-feed"></div>
  38. </div>
  39. <script src="../lib/require.js"></script>
  40. <script>
  41. require({
  42. waitSeconds : 120, //make sure it is enough to load all gmaps scripts
  43. paths : {
  44. async : '../src/async' //alias to plugin
  45. }
  46. });
  47. // you can use a "!callbackName" at the end of the URL
  48. // to specify name of parameter that expects callback function name
  49. // the default value is "!callback" if not present.
  50. // Notice that flickr uses "!jsoncallback".
  51. require(
  52. [
  53. 'async!http://api.flickr.com/services/feeds/photos_public.gne?id=27041612@N06&format=json!jsoncallback',
  54. 'async!http://maps.google.com/maps/api/js?sensor=false'
  55. ],
  56. function(photos){
  57. //flickr
  58. var flickrDiv = document.getElementById('flickr-feed'),
  59. idx = Math.round((photos.items.length - 1) * Math.random());
  60. flickrDiv.innerHTML += photos.items[idx].description;
  61. //Google maps is available and all components are ready to use.
  62. var mapDiv = document.getElementById('map-canvas');
  63. var map = new google.maps.Map(mapDiv, {
  64. center: new google.maps.LatLng(37.4419, -122.1419),
  65. zoom: 13,
  66. mapTypeId: google.maps.MapTypeId.ROADMAP,
  67. navigationControl: true,
  68. navigationControlOptions: {
  69. style: google.maps.NavigationControlStyle.SMALL
  70. }
  71. });
  72. }
  73. );
  74. </script>
  75. </body>
  76. </html>