This repository was archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.getScripts.js
More file actions
88 lines (74 loc) · 2.06 KB
/
jquery.getScripts.js
File metadata and controls
88 lines (74 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* getScripts (jQuery shorthand extended)
* A lightweight asynchronous JavaScript loader.
*
* Copyright 2015, Marc S. Brooks (https://mbrooks.info)
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Dependencies:
* jquery.js
*/
if (!window.jQuery || (window.jQuery && parseInt(window.jQuery.fn.jquery.replace('.', '')) < parseInt('1.8.3'.replace('.', '')))) {
throw new Error('jQuery.getScripts requires jQuery 1.8.3 or greater.');
}
(function($) {
/**
* @namespace getScripts
*/
var methods = {
/**
* Create new instance of getScripts
*
* @memberof getScripts
* @method init
*
* @example
* jQuery.getScripts(files, callback);
*
* @param {Array} files
* @param {Function} callback
*/
"init": function(files, callback) {
// Support method chaining.
return this.each(function() {
if (!$.isArray(files) || files.length === 0) {
$.error('Invalid array');
}
if (callback && !$.isFunction(callback)) {
$.error('Invalid callback function');
}
var count = 0;
// Execute as recursive callback.
var _callback = function() {
if (files.length == count) {
if (callback) {
callback();
}
return true;
}
var script = files[count];
$.getScript(script)
.done(_callback)
.fail(function(jqxhr, settings, exception) {
$.error("Script '" + script + "' failed with exception\n" + exception);
});
count++;
};
_callback();
});
}
};
$.fn.getScripts = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else
if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}
else {
$.error('Method ' + method + ' does not exist in jQuery.getScripts');
}
};
})(jQuery);