798013595916eab2771d53c9c08bb9c970f1b671Ryan Grove<div class="intro">
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThe Get Utility makes it easy to dynamically load JavaScript and CSS resources and be notified when they've finished loading. Get is used internally by the <a href="../yui/index.html">YUI Loader</a> to load YUI modules and by the <a href="../jsonp/index.html">JSONP module</a> to make JSONP requests. The Get Utility is transactional in nature and is capable of loading multiple resources either serially or in parallel.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove{{>getting-started}}
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h2>Using the Get Utility</h2>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h3>Loading CSS and JavaScript</h3>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h4>`js()`</h4>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveTo load a JavaScript resource, pass a URL (both relative and absolute URLs are fine) and a callback function to the <a href="{{apiDocs}}/classes/Get.html#method_js">`Y.Get.js()`</a> method. Get will execute the callback function when the resource has finished loading. If an error occurs, the first argument passed to the callback will be an array of error objects.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove`Y.Get` is a static class, so you don't need to instantiate it before calling its methods.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove// Load a single JavaScript resource.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveY.Get.js('http://example.com/file.js', function (err) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log('Error loading JS: ' + err[0].error, 'error');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h4>`css()`</h4>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveLoading CSS works the same way as JS, but you call the <a href="{{apiDocs}}/classes/Get.html#method_css">`css()`</a> method instead of `js()`:
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove// Load a single CSS resource.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log('Error loading CSS: ' + err[0].error, 'error');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h4>`load()`</h4>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveIf you want to load more than one resource in a single transaction and be notified when all of them have finished loading, you may pass an array of URLs to `css()` or `js()`.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove// Load multiple JS resources and execute the callback after all have finished
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveY.Get.js(['one.js', 'two.js', 'http://example.com/three.js'], function (err) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.Array.each(err, function (error) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log('Error loading JS: ' + error.error, 'error');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log('All JS files loaded successfully!');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveYou can even mix and match both JavaScript and CSS files in the same transaction by calling <a href="{{apiDocs}}/classes/Get.html#method_load">`Y.Get.load()`</a>, which will guess the type of each URL by looking at the file extension.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove// Load both CSS and JS in a single transaction and execute the callback after
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove// all resources have finished loading.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveY.Get.load(['widget.js', 'widget.css'], function (err) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.Array.each(err, function (error) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log('Error loading file: ' + error.error, 'error');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log('All CSS and JS files loaded successfully!');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThis should be enough to get you started using the Get Utility, but keep reading to learn about Get's more advanced functionality.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h3>Serial vs. Parallel Loading</h3>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThe `css()`, `js()`, and `load()` methods execute <em>asynchronously</em>, which means that they return instantly rather than blocking subsequent code until the operation finishes. This is why it's necessary to provide a callback function if you want to be notified when loading is complete.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveIt's also important to understand when resources will be loaded <em>serially</em> (one after another) versus in <em>parallel</em> (all at the same time). Loading resources in parallel is always faster than loading them serially, but can sometimes be dangerous because resources won't always finish loading in the same order they were requested.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThe Get Utility always does the safest thing by default:
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveTransactions are processed serially. This means that only one transaction is in progress at a time, and it must finish before the next transaction will begin. Each call to `css()`, `js()`, or `load()` creates a transaction, although a single transaction may involve multiple requests.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveWithin a transaction, CSS resources are always loaded in parallel. This is because, with CSS, load order has no effect on the end result, since style precedence is determined by the order of `<link>` nodes in the DOM.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveWithin a transaction, JS resources are loaded in parallel in Firefox and Opera, because these browsers are capable of preserving script execution order regardless of load order. In all other browsers, JS resources are loaded serially unless the <a href="{{apiDocs}}/classes/Get.html#property_options">`async` option</a> is set to `true`.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveWhen the <a href="{{apiDocs}}/classes/Get.html#property_options">`async` option</a> is set to `true`, JS resources are loaded in parallel regardless of the browser, and execution order is not guaranteed.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h3>Working With Transactions</h3>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveA transaction object is an instance of the <a href="{{apiDocs}}/classes/Get.Transaction.html">`Y.Get.Transaction`</a> class. It contains methods and properties that allow you to inspect and manipulate a transaction, which encompasses one or more CSS or JS requests and a set of config options associated with those requests.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThe `css()`, `js()`, and `load()` methods each return a transaction object. This same transaction object is also passed as the second argument to the completion callback.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove// Load a script and assign the transaction object to a var.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grovevar tx = Y.Get.js('hello.js', function (err, tx) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove // The transaction object is also passed as the second arg to completion
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove // callbacks.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveIt's not necessary to hold onto a transaction object unless you want to be able to abort a request before it finishes or manually purge `<script>` or `<link>` nodes created by the transaction.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h4>Aborting Transactions</h4>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveYou can abort a transaction in progress by calling the transaction's <a href="{{apiDocs}}/classes/Get.Transaction.html#method_abort">`abort()`</a> method.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grovevar tx = Y.Get.js(['one.js', 'two.js'], function (err) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log(err[0].error, 'error');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grovetx.abort(); // Results in the log message "Aborted."
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GrovePass a string to the `abort()` method to customize the error message passed to callbacks.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveNote that since browsers don't expose a safe API for aborting in-progress JS and CSS requests, the Get Utility can't actually stop requests that have already started. Calling `abort()` will immediately cause callbacks to execute and will cancel any pending requests within the transaction that haven't yet begun, but if there are any requests already in progress, the browser will finish them silently.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h4>Purging Inserted Nodes</h4>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveIf you plan to load lots of JavaScript resources—for example, maybe your app makes frequent JSONP requests to a remote API—you'll end up creating lots of `<script>` nodes behind the scenes. Each node on the page uses a small amount of memory, and since the actual script nodes aren't usually needed once the JS has been executed, it's a good idea to clean them up occasionally by purging them.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveBy default, the Get Utility will automatically purge script nodes after every 20 requests. This number is relatively high since purging incurs a slight processing cost. If you want to manually purge the nodes inserted by a transaction instead of waiting for the automatic purge, you can do so by calling the transaction's `purge()` method.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveY.Get.js(['one.js', 'two.js'], function (err, tx) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove // Purge all the DOM nodes created by this transaction.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveYou can customize the automatic purge threshold by setting the <a href="{{apiDocs}}/classes/Get.html#property_options">`purgethreshold`</a> config option, and you can disable automatic purging completely by setting the <a href="{{apiDocs}}/classes/Get.html#property_options">`autopurge`</a> option to `false`.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveNote that the Get Utility will not automatically purge CSS `<link>` nodes by default, since removing a `<link>` node from the DOM also removes any styles it applied. Calling `purge()` manually on a transaction that included CSS requests will purge the `<link>` nodes and remove styles, so be careful.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h4>Manually Executing Transactions</h4>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveAs described in [[#Serial vs. Parallel Loading]], the Get Utility executes transactions serially (one after the other) to ensure that they don't conflict with one another. If for some reason you want multiple transactions to execute in parallel and you're willing to take your life into your own hands, you can manually start a transaction by calling its `execute()` method.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove// txOne is started automatically, and we can manually start txTwo in parallel.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveCalling `execute()` on a transaction that's already in progress or has already finished is safe and won't restart the transaction.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveAn additional feature of `execute()` is that it accepts a callback function, which works just like the callback function passed to `css()`, `js()`, or `load()`. In fact, you can even call `execute()` multiple times to register multiple callback functions, and they'll be queued and executed in order once the transaction finishes. If you call `execute()` with a callback function on a transaction that's already finished, the callback will be executed immediately.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h3>Configuration Options</h3>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThe Get Utility supports the following config options. All options may be set at the transaction level. Some options may also be set at the request level (i.e., for a specific URL within a transaction). Options that may be set at the request level are indicated by a "Y" in the "Request?" column.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <th>Name</th>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <th>Default</th>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <th>Request?</th>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <th>Description</th>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`async`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`false`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td style="text-align:center">Y</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Whether or not to load scripts asynchronously, meaning they're requested in parallel and execution order is not guaranteed. Has no effect on CSS, since CSS is always loaded asynchronously.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`attributes`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<pre class="">
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove charset: "utf-8",
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove id : <em>auto</em>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td style="text-align:center">Y</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove HTML attribute name/value pairs that should be added to inserted nodes. By default, the `charset` attribute will be set to "utf-8" and nodes will be given an auto-generated `id` attribute, but you can override these with your own values if desired.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`autopurge`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove `true` for JS<br>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove `false` for CSS
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Whether or not to automatically purge inserted nodes after the purge threshold is reached. This is `true` by default for JavaScript, but `false` for CSS since purging a CSS node will also remove any styling applied by the referenced file.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`context`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td><em>transaction object</em></td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove `this` object to use when calling callback functions. Defaults to the transaction object.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`data`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`undefined`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Arbitrary data object to pass to "on*" callbacks.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`doc`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td style="text-align:center">Y</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Document into which nodes should be inserted. By default, the current document is used.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`insertBefore`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td><em>auto</em></td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td style="text-align:center">Y</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove HTML element or id string of an element before which all generated nodes should be inserted. If not specified, Get will automatically determine the best place to insert nodes for maximum compatibility.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`onEnd`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`undefined`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Callback to execute after a transaction is complete, regardless of whether it succeeded or failed.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`onFailure`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`undefined`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Callback to execute after a transaction fails, times out, or is aborted.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`onProgress`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`undefined`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Callback to execute after each individual request in a transaction either succeeds or fails.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`onSuccess`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`undefined`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Callback to execute after a transaction completes successfully with no errors. Note that in browsers that don't support the `error` event on CSS `<link>` nodes, a failed CSS request may still be reported as a success because in these browsers it can be difficult or impossible to distinguish between success and failure for CSS resources.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`onTimeout`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`undefined`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Callback to execute after a transaction times out.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`pollInterval`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`50`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Polling interval (in milliseconds) for detecting CSS load completion in browsers that don't support the `load` event on `<link>` nodes. This isn't used for JavaScript.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`purgethreshold`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`20`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Number of nodes to insert before triggering an automatic purge when `autopurge` is `true`.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`timeout`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`undefined`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Number of milliseconds to wait before aborting a transaction. When a timeout occurs, the `onTimeout` callback is called, followed by `onFailure` and finally `onEnd`. By default, there is no timeout.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td>`type`</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td><em>auto</em></td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove <td style="text-align:center">Y</td>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Resource type ("css" or "js"). This option is set automatically by the `css()` and `js()` functions and will be ignored there, but may be useful when using the `load()` function. If not specified, the type will be inferred from the URL, defaulting to "js" if the URL doesn't contain a recognizable file extension.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h4>Using Transaction-Specific Options</h4>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveTransaction-specific configuration options apply only to a single transaction. To specify one or more transaction-specific options, just pass a config object as the second argument to `css()`, `js()` or `load()`.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove attributes: {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove 'charset': 'shift-jis', // custom charset attribute for inserted DOM nodes
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove 'class' : 'intl' // custom 'class' attribute for inserted DOM nodes
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove timeout: 10000 // timeout after 10 seconds
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove}, function (err) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveYou may have noticed that in the example above, the options object is the second argument and the callback is the third argument, whereas previous examples pass a callback as the second argument and no options object. The `css()`, `js()`, and `load()` methods support both signatures for convenience. The only required argument is the first one.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h4>Using Request-Specific Options</h4>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveCertain config options (see [[#Configuration Options|the table above]] for a complete list) can be specified on a per-request basis, meaning they'll apply only to a single URL.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveTo specify request-specific options, pass an object or array of objects to `css()`, `js()`, or `load()` instead of passing a string or array of strings. Each object must have a `url` property specifying the URL to load, and may also contain request-specific options. You can freely mix and match string URLs and objects if desired.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove {url: 'thing-one.js', attributes: {id: 'thing-one'}},
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove {url: 'thing-two.js', attributes: {id: 'thing-two'}, async: true}
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove], function (err) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveWhen both request-specific options and transaction-specific options are specified, the options will be merged per request, with request-specific options taking precedence when there are collisions.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h3>Granular Callbacks</h3>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveWhile the callback parameter of the `css()`, `js()`, and `load()` methods makes it easy to define a combined success/failure callback for a transaction, there are times when more granularity is needed. Perhaps you want to use separate callbacks for success and failure, or perhaps you want to be notified of the progress of each request in a transaction rather than waiting until the entire transaction is complete. That's where granular callbacks come in.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThe Get Utility supports five different granular callbacks per transaction: `onEnd`, `onFailure`, `onProgress`, `onSuccess`, and `onTimeout`. See [[#Configuration Options]] for descriptions of when each callback is called.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveGranular callbacks receive the transaction object as an argument.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove onFailure: function () {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log('Failure!');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove onSuccess: function () {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log('Success!');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveYou can pass arbitrary data to your callbacks by setting the `data` option on the transaction.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove data: {progress: 0},
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove onProgress: function (tx) {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove Y.log('Loaded ' + tx.data.progress + ' file(s)');
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveBy default, the `this` object inside a granular callback refers to the transaction object, but you can customize it by setting the `context` option.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove context: {bark: 'ruff ruff!'},
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove onSuccess: function () {
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h2>Using JSONP APIs</h2>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveA common way to consume a web service that returns JSON data is to use a convention called <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>. The remote service returns data wrapped in a JavaScript function call (the name of which is supplied in the request), so retrieving the data is simple as loading and executing the JSONP URL as if it were a script. When the returned JS executes, the data is passed to the named function.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThe Get Utility can be used to consume JSONP APIs by loading JSONP URLs as scripts. However, the <a href="../jsonp/index.html">JSONP Utility</a> (which uses Get under the hood) provides a simplified API for making JSONP requests, so we recommend using that component for JSONP rather than using Get directly.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<h2>How is the Get Utility Different From IO?</h2>
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveIn simple terms, the Get Utility loads new JS or CSS resources into a document by creating new DOM nodes and setting the `src` or `href` attribute. Files loaded in this manner are processed (and, in the case of scripts, executed) as soon as they load.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan Grove<p>While query parameters can be passed in the URL, no data can be sent to the server via HTTP POST using this method; the Get Utility can only make HTTP GET requests. This makes the Get Utility ideal for loading scripts or CSS progressively (lazy loading) or for retrieving cross-domain JSON data from trusted sources, but somewhat less ideal for more sophisticated communication.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThe basic version of the <a href="../io/index.html">IO Utility</a> (`io-base`) uses the `XMLHttpRequest` object to interact with the server. `XMLHttpRequest` is limited by a strict <a href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>, but it supports a greater range of HTTP methods (including POST). As a result, IO is a more appropriate choice for rich two-way communication between browser and server and gives you more control over data before it's processed within the browser.
7a45bf6054b9f5959fd29cffd9b1b9158b242274Ryan GroveThe IO Utility also supports cross domain requests through the `io-xdr` module. However, there are specific trust requirements as described in the documentation for the <a href="../io/index.html#xdr">IO Utility</a>. The `io-xdr` submodule may be a better choice than the Get Utility for cross-domain communication if the service you are accessing can be configured to trust the server that is hosting your application.