jsonp-gallery.mustache revision 819e90d415ed17d59af3a247b2ad9d6feb0c21b5
1127N/A href="http://yuilibrary.com">YUILibrary</a> to fetch information about
1127N/A<p>The structure of the JavaScript object returned from YUILibrary's JSONP service will look like this:</p>
1127N/A<p>We'll use these objects to populate an HTML template with data <em>{placeholder}</em>s using `Y.Lang.sub( template, object )`.</p>
1127N/A<p>But since each call to `Y.jsonp()` creates a new instance of `Y.JSONPRequest`, we may as well store the instance and reuse it.</p>
1127N/Avar gallery = new Y.JSONPRequest("http://yuilibrary.com/gallery/api/random?callback={callback}", handleJSONP);
1127N/A<p>JSONPRequest doesn't have any built-in polling mechanism, but `Y.later()` can handle this for us.</p>
1127N/Avar url = "http://yuilibrary.com/gallery/api/random?callback={callback}";
1127N/Avar gallery = new Y.JSONPRequest(url, handleJSONP);
<p>In case the Gallery API is busy or some other problem arises, we'll also want to handle this case and display an error. We can do this by passing a configuration object as the second parameter rather than a simple success callback.</p>
var gallery = new Y.JSONPRequest(url, {
Y.one("#out").setContent( failureTemplate );
gallery.send();
<p>Now we'll add a bit of flourish, by adding a visual indicator of how long until the next module is requested. We'll replace the call to `Y.later()` with a call to `node.transition()` using a shrinking border to show the remaining time. Then when the transition is complete, we call `send()` again.
var gallery = new Y.JSONPRequest(url, {
Y.one("#out h4")
gallery.send();
Y.one("#out").setContent( failureTemplate );
gallery.send();
<p>The final step is to add the ability to start and stop the polling. We'll manage this by adding a property to the `gallery` JSONPRequest instance named `gallery.polling`. See the full code listing below for the implementation.