1
/* Copyright (c) 2010, Canonical Ltd. All rights reserved. */
4
* Prefetch a set of files, deferring calls to 'use' until the
5
* preloaded files are finished loading.
10
YUI.prototype.prefetch = function prefetch() {
14
pending = arguments.length;
16
var YArray_each = Y.Array.each,
17
YGet_script = Y.Get.script;
20
* Wrap the native 'use' function to add some smarts around
21
* our custom loading of the rolled-up minified files so that
22
* we delay the loader from firing until the rolled-up files
23
* are finished loading, in order to avoid loading the
26
var native_use = Y.use;
28
/* Now replace the original 'use' function with our own. */
29
Y.use = function use() {
31
* If all external dependencies have been loaded, just
32
* call the native 'use' function directly.
35
native_use.apply(Y, arguments);
38
* If there are things still loading, queue calls to 'use'
39
* until they are finished.
41
var ridx = arguments.length,
43
/* Make a copy of the original arguments. */
45
args[ridx] = arguments[ridx];
48
/* Push copied arguments into the queue. */
54
* For each item to be preloaded, use the Y.Get utility to
55
* fetch the script (which might fetch them in parallel). When
56
* all the scripts are finished loading, we'll process the
57
* deferred calls to use with the native 'use' function.
59
YArray_each(preload, function(value) {
60
YGet_script(value, {onEnd: function() {
62
* Once an item has finished preloading, we decrement
63
* the pending variable. Once it reaches zero, we
64
* know all preload items have finished loading.
69
* Once we're done, restore the original 'use'
70
* function and call all of the deferred callbacks in
71
* their original order.
77
* Attach the 'loader' module, which *should* be
78
* already loaded by now.
80
Y._attach(["loader"]);
81
YArray_each(deferred, function(value) {
82
native_use.apply(this, value);
85
}, attributes: {defer: "defer"}});