Comparing version 1.0.4 to 2.0.0
@@ -5,1 +5,2 @@ Authors ordered by first contribution. | ||
revyh https://github.com/revyh | ||
Ryan Burke https://github.com/codeburke |
{ | ||
"name": "loadjs", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "authors": [ |
# LoadJS Changelog | ||
## 2.0.0 - June 15, 2016 | ||
* Changed API to accept object with success/fail functions | ||
* Added support for async: false | ||
## 1.0.4 - May 25, 2016 | ||
@@ -4,0 +9,0 @@ |
@@ -83,3 +83,3 @@ loadjs = (function () { | ||
*/ | ||
function loadScript(path, callbackFn) { | ||
function loadScript(path, callbackFn, async) { | ||
var doc = document, | ||
@@ -89,2 +89,3 @@ s = doc.createElement('script'); | ||
s.src = path; | ||
s.async = (async === undefined) ? true : async; | ||
@@ -106,7 +107,7 @@ s.onload = s.onerror = s.onbeforeload = function(ev) { | ||
*/ | ||
function loadScripts(paths, callbackFn) { | ||
function loadScripts(paths, callbackFn, async) { | ||
// listify paths | ||
paths = paths.push ? paths : [paths]; | ||
var i = paths.length, numWaiting = i, pathsNotFound = [], fn; | ||
var numWaiting = paths.length, x = numWaiting, pathsNotFound = [], fn, i; | ||
@@ -130,3 +131,3 @@ // define callback function | ||
// load scripts | ||
while (i--) loadScript(paths[i], fn); | ||
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async); | ||
} | ||
@@ -142,12 +143,11 @@ | ||
*/ | ||
function loadjs(paths, arg1, arg2, arg3) { | ||
var bundleId, successFn, failFn; | ||
function loadjs(paths, arg1, arg2) { | ||
var bundleId, args; | ||
// bundleId | ||
if (arg1 && !arg1.call) bundleId = arg1; | ||
// bundleId (if string) | ||
if (arg1 && arg1.trim) bundleId = arg1; | ||
// args (default is {}) | ||
args = (bundleId ? arg2 : arg1) || {}; | ||
// successFn, failFn | ||
successFn = bundleId ? arg2 : arg1; | ||
failFn = bundleId ? arg3 : arg2; | ||
// throw error if bundle is already defined | ||
@@ -164,8 +164,9 @@ if (bundleId) { | ||
loadScripts(paths, function(pathsNotFound) { | ||
if (pathsNotFound.length) (failFn || devnull)(pathsNotFound); | ||
else (successFn || devnull)(); | ||
// success and fail callbacks | ||
if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound); | ||
else (args.success || devnull)(); | ||
// publish bundle load event | ||
publish(bundleId, pathsNotFound); | ||
}); | ||
}, args.async); | ||
} | ||
@@ -177,11 +178,10 @@ | ||
* @param {(string|string[])} deps - List of bundle ids | ||
* @param {Function} [successFn] - Success callback | ||
* @param {Function} [failFn] - Fail callback | ||
* @param {Object} args - success/fail arguments | ||
*/ | ||
loadjs.ready = function (deps, successFn, failFn) { | ||
loadjs.ready = function (deps, args) { | ||
// subscribe to bundle load event | ||
subscribe(deps, function(depsNotFound) { | ||
// execute callbacks | ||
if (depsNotFound.length) (failFn || devnull)(depsNotFound); | ||
else (successFn || devnull)(); | ||
if (depsNotFound.length) (args.fail || devnull)(depsNotFound); | ||
else (args.success || devnull)(); | ||
}); | ||
@@ -188,0 +188,0 @@ |
@@ -1,1 +0,1 @@ | ||
loadjs=function(){function n(n,e){n=n.push?n:[n];var t,o,r,f,u=[],a=n.length,l=a;for(t=function(n,t){t.length&&u.push(n),l--,l||e(u)};a--;)o=n[a],r=i[o],r?t(o,r):(f=c[o]=c[o]||[],f.push(t))}function e(n,e){if(n){var t=c[n];if(i[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e){var t=document,o=t.createElement("script");o.src=n,o.onload=o.onerror=o.onbeforeload=function(t){e(n,t.type[0],t.defaultPrevented)},t.head.appendChild(o)}function o(n,e){n=n.push?n:[n];var o,r=n.length,f=r,u=[];for(o=function(n,t,o){if("e"==t&&u.push(n),"b"==t){if(!o)return;u.push(n)}f--,f||e(u)};r--;)t(n[r],o)}function r(n,t,r,i){var c,a,l;if(t&&!t.call&&(c=t),a=c?r:t,l=c?i:r,c){if(c in u)throw new Error("LoadJS: Bundle already defined");u[c]=!0}o(n,function(n){n.length?(l||f)(n):(a||f)(),e(c,n)})}var f=function(){},u={},i={},c={};return r.ready=function(e,t,o){return n(e,function(n){n.length?(o||f)(n):(t||f)()}),r},r.done=function(n){e(n,[])},r}(); | ||
loadjs=function(){function n(n,e){n=n.push?n:[n];var t,o,r,i,f=[],a=n.length,s=a;for(t=function(n,t){t.length&&f.push(n),s--,s||e(f)};a--;)o=n[a],r=u[o],r?t(o,r):(i=c[o]=c[o]||[],i.push(t))}function e(n,e){if(n){var t=c[n];if(u[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e,t){var o=document,r=o.createElement("script");r.src=n,r.async=void 0===t?!0:t,r.onload=r.onerror=r.onbeforeload=function(t){e(n,t.type[0],t.defaultPrevented)},o.head.appendChild(r)}function o(n,e,o){n=n.push?n:[n];var r,i,f=n.length,u=f,c=[];for(r=function(n,t,o){if("e"==t&&c.push(n),"b"==t){if(!o)return;c.push(n)}f--,f||e(c)},i=0;u-1>=i;i++)t(n[i],r,o)}function r(n,t,r){var u,c;if(t&&t.trim&&(u=t),c=(u?r:t)||{},u){if(u in f)throw new Error("LoadJS: Bundle already defined");f[u]=!0}o(n,function(n){n.length?(c.fail||i)(n):(c.success||i)(),e(u,n)},c.async)}var i=function(){},f={},u={},c={};return r.ready=function(e,t){return n(e,function(n){n.length?(t.fail||i)(n):(t.success||i)()}),r},r.done=function(n){e(n,[])},r}(); |
@@ -91,3 +91,3 @@ (function(root, factory) { | ||
*/ | ||
function loadScript(path, callbackFn) { | ||
function loadScript(path, callbackFn, async) { | ||
var doc = document, | ||
@@ -97,2 +97,3 @@ s = doc.createElement('script'); | ||
s.src = path; | ||
s.async = (async === undefined) ? true : async; | ||
@@ -114,7 +115,7 @@ s.onload = s.onerror = s.onbeforeload = function(ev) { | ||
*/ | ||
function loadScripts(paths, callbackFn) { | ||
function loadScripts(paths, callbackFn, async) { | ||
// listify paths | ||
paths = paths.push ? paths : [paths]; | ||
var i = paths.length, numWaiting = i, pathsNotFound = [], fn; | ||
var numWaiting = paths.length, x = numWaiting, pathsNotFound = [], fn, i; | ||
@@ -138,3 +139,3 @@ // define callback function | ||
// load scripts | ||
while (i--) loadScript(paths[i], fn); | ||
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async); | ||
} | ||
@@ -150,12 +151,11 @@ | ||
*/ | ||
function loadjs(paths, arg1, arg2, arg3) { | ||
var bundleId, successFn, failFn; | ||
function loadjs(paths, arg1, arg2) { | ||
var bundleId, args; | ||
// bundleId | ||
if (arg1 && !arg1.call) bundleId = arg1; | ||
// bundleId (if string) | ||
if (arg1 && arg1.trim) bundleId = arg1; | ||
// args (default is {}) | ||
args = (bundleId ? arg2 : arg1) || {}; | ||
// successFn, failFn | ||
successFn = bundleId ? arg2 : arg1; | ||
failFn = bundleId ? arg3 : arg2; | ||
// throw error if bundle is already defined | ||
@@ -172,8 +172,9 @@ if (bundleId) { | ||
loadScripts(paths, function(pathsNotFound) { | ||
if (pathsNotFound.length) (failFn || devnull)(pathsNotFound); | ||
else (successFn || devnull)(); | ||
// success and fail callbacks | ||
if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound); | ||
else (args.success || devnull)(); | ||
// publish bundle load event | ||
publish(bundleId, pathsNotFound); | ||
}); | ||
}, args.async); | ||
} | ||
@@ -185,11 +186,10 @@ | ||
* @param {(string|string[])} deps - List of bundle ids | ||
* @param {Function} [successFn] - Success callback | ||
* @param {Function} [failFn] - Fail callback | ||
* @param {Object} args - success/fail arguments | ||
*/ | ||
loadjs.ready = function (deps, successFn, failFn) { | ||
loadjs.ready = function (deps, args) { | ||
// subscribe to bundle load event | ||
subscribe(deps, function(depsNotFound) { | ||
// execute callbacks | ||
if (depsNotFound.length) (failFn || devnull)(depsNotFound); | ||
else (successFn || devnull)(); | ||
if (depsNotFound.length) (args.fail || devnull)(depsNotFound); | ||
else (args.success || devnull)(); | ||
}); | ||
@@ -196,0 +196,0 @@ |
@@ -83,3 +83,3 @@ loadjs = (function () { | ||
*/ | ||
function loadScript(path, callbackFn) { | ||
function loadScript(path, callbackFn, async) { | ||
var doc = document, | ||
@@ -89,2 +89,3 @@ s = doc.createElement('script'); | ||
s.src = path; | ||
s.async = (async === undefined) ? true : async; | ||
@@ -106,7 +107,7 @@ s.onload = s.onerror = s.onbeforeload = function(ev) { | ||
*/ | ||
function loadScripts(paths, callbackFn) { | ||
function loadScripts(paths, callbackFn, async) { | ||
// listify paths | ||
paths = paths.push ? paths : [paths]; | ||
var i = paths.length, numWaiting = i, pathsNotFound = [], fn; | ||
var numWaiting = paths.length, x = numWaiting, pathsNotFound = [], fn, i; | ||
@@ -130,3 +131,3 @@ // define callback function | ||
// load scripts | ||
while (i--) loadScript(paths[i], fn); | ||
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async); | ||
} | ||
@@ -142,12 +143,11 @@ | ||
*/ | ||
function loadjs(paths, arg1, arg2, arg3) { | ||
var bundleId, successFn, failFn; | ||
function loadjs(paths, arg1, arg2) { | ||
var bundleId, args; | ||
// bundleId | ||
if (arg1 && !arg1.call) bundleId = arg1; | ||
// bundleId (if string) | ||
if (arg1 && arg1.trim) bundleId = arg1; | ||
// args (default is {}) | ||
args = (bundleId ? arg2 : arg1) || {}; | ||
// successFn, failFn | ||
successFn = bundleId ? arg2 : arg1; | ||
failFn = bundleId ? arg3 : arg2; | ||
// throw error if bundle is already defined | ||
@@ -164,8 +164,9 @@ if (bundleId) { | ||
loadScripts(paths, function(pathsNotFound) { | ||
if (pathsNotFound.length) (failFn || devnull)(pathsNotFound); | ||
else (successFn || devnull)(); | ||
// success and fail callbacks | ||
if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound); | ||
else (args.success || devnull)(); | ||
// publish bundle load event | ||
publish(bundleId, pathsNotFound); | ||
}); | ||
}, args.async); | ||
} | ||
@@ -177,11 +178,10 @@ | ||
* @param {(string|string[])} deps - List of bundle ids | ||
* @param {Function} [successFn] - Success callback | ||
* @param {Function} [failFn] - Fail callback | ||
* @param {Object} args - success/fail arguments | ||
*/ | ||
loadjs.ready = function (deps, successFn, failFn) { | ||
loadjs.ready = function (deps, args) { | ||
// subscribe to bundle load event | ||
subscribe(deps, function(depsNotFound) { | ||
// execute callbacks | ||
if (depsNotFound.length) (failFn || devnull)(depsNotFound); | ||
else (successFn || devnull)(); | ||
if (depsNotFound.length) (args.fail || devnull)(depsNotFound); | ||
else (args.success || devnull)(); | ||
}); | ||
@@ -188,0 +188,0 @@ |
@@ -1,1 +0,1 @@ | ||
loadjs=function(){function n(n,e){n=n.push?n:[n];var t,o,r,f,u=[],a=n.length,l=a;for(t=function(n,t){t.length&&u.push(n),l--,l||e(u)};a--;)o=n[a],r=i[o],r?t(o,r):(f=c[o]=c[o]||[],f.push(t))}function e(n,e){if(n){var t=c[n];if(i[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e){var t=document,o=t.createElement("script");o.src=n,o.onload=o.onerror=o.onbeforeload=function(t){e(n,t.type[0],t.defaultPrevented)},t.head.appendChild(o)}function o(n,e){n=n.push?n:[n];var o,r=n.length,f=r,u=[];for(o=function(n,t,o){if("e"==t&&u.push(n),"b"==t){if(!o)return;u.push(n)}f--,f||e(u)};r--;)t(n[r],o)}function r(n,t,r,i){var c,a,l;if(t&&!t.call&&(c=t),a=c?r:t,l=c?i:r,c){if(c in u)throw new Error("LoadJS: Bundle already defined");u[c]=!0}o(n,function(n){n.length?(l||f)(n):(a||f)(),e(c,n)})}var f=function(){},u={},i={},c={};return r.ready=function(e,t,o){return n(e,function(n){n.length?(o||f)(n):(t||f)()}),r},r.done=function(n){e(n,[])},r}(); | ||
loadjs=function(){function n(n,e){n=n.push?n:[n];var t,o,r,i,f=[],a=n.length,s=a;for(t=function(n,t){t.length&&f.push(n),s--,s||e(f)};a--;)o=n[a],r=u[o],r?t(o,r):(i=c[o]=c[o]||[],i.push(t))}function e(n,e){if(n){var t=c[n];if(u[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e,t){var o=document,r=o.createElement("script");r.src=n,r.async=void 0===t?!0:t,r.onload=r.onerror=r.onbeforeload=function(t){e(n,t.type[0],t.defaultPrevented)},o.head.appendChild(r)}function o(n,e,o){n=n.push?n:[n];var r,i,f=n.length,u=f,c=[];for(r=function(n,t,o){if("e"==t&&c.push(n),"b"==t){if(!o)return;c.push(n)}f--,f||e(c)},i=0;u-1>=i;i++)t(n[i],r,o)}function r(n,t,r){var u,c;if(t&&t.trim&&(u=t),c=(u?r:t)||{},u){if(u in f)throw new Error("LoadJS: Bundle already defined");f[u]=!0}o(n,function(n){n.length?(c.fail||i)(n):(c.success||i)(),e(u,n)},c.async)}var i=function(){},f={},u={},c={};return r.ready=function(e,t){return n(e,function(n){n.length?(t.fail||i)(n):(t.success||i)()}),r},r.done=function(n){e(n,[])},r}(); |
{ | ||
"name": "loadjs", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
@@ -23,3 +23,3 @@ "description": "Tiny async loader for modern browsers", | ||
"del": "2.2.0", | ||
"gulp": "git+https://github.com/gulpjs/gulp#c4a7542986107949b049fc81aa081d72d95e98ff", | ||
"gulp": "git+https://github.com/gulpjs/gulp#4ed9a4a3275559c73a396eff7e1fde3824951ebb", | ||
"gulp-jshint": "2.0.1", | ||
@@ -26,0 +26,0 @@ "gulp-rename": "1.2.2", |
177
README.md
@@ -5,3 +5,3 @@ # LoadJS | ||
LoadJS is a tiny async loader for modern browsers (537 bytes). | ||
LoadJS is a tiny async loader for modern browsers (585 bytes). | ||
@@ -13,5 +13,5 @@ [![Dependency Status](https://david-dm.org/muicss/loadjs.svg)](https://david-dm.org/muicss/loadjs) | ||
LoadJS is a tiny async loading library for modern browsers (IE9+). It has a simple yet powerful dependency management system that lets you fetch files in parallel and execute code after the dependencies have been met. The recommended way to use LoadJS is to include the minified source code in your <html> and then use the `loadjs` global to manage JavaScript dependencies after pageload. | ||
LoadJS is a tiny async loading library for modern browsers (IE10+). It has a simple yet powerful dependency management system that lets you fetch files in parallel and execute code after the dependencies have been met. The recommended way to use LoadJS is to include the minified source code in your <html> and then use the `loadjs` global to manage JavaScript dependencies after pageload. | ||
LoadJS is based on the excellent <a href="https://github.com/ded/script.js">$script</a> library by <a href="https://github.com/ded">Dustin Diaz</a>. We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/failure callbacks and to optimize the library for modern browsers. LoadJS is 537 bytes (minified + gzipped). | ||
LoadJS is based on the excellent <a href="https://github.com/ded/script.js">$script</a> library by <a href="https://github.com/ded">Dustin Diaz</a>. We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/failure callbacks and to optimize the library for modern browsers. LoadJS is 586 bytes (minified + gzipped). | ||
@@ -25,4 +25,9 @@ Here's an example of what you can do with LoadJS: | ||
// execute code elsewhere when the bundle has loaded | ||
loadjs.ready('foobar', function() { | ||
// foo.js & bar.js loaded | ||
loadjs.ready('foobar', { | ||
success: function() { | ||
// foo.js & bar.js loaded | ||
}, | ||
fail: function(depsNotFound) { | ||
// foobar bundle load failed | ||
} | ||
}); | ||
@@ -46,4 +51,5 @@ ``` | ||
loadjs.ready('foobar', function() { | ||
// foo.js & bar.js loaded | ||
loadjs.ready('foobar', { | ||
success: function() { /* foo.js & bar.js loaded */ }, | ||
fail: function(depsNotFound) {/* foobar bundle load failed */} | ||
}); | ||
@@ -54,3 +60,3 @@ ``` | ||
* IE9+ | ||
* IE10+ | ||
* Opera 12+ | ||
@@ -63,3 +69,3 @@ * Safari 5+ | ||
LoadJS also detects script failures from AdBlock Plus and Ghostery in: | ||
LoadJS also detects script failures from AdBlock Plus and Ghostery in: | ||
@@ -71,91 +77,112 @@ * Safari | ||
```javascript | ||
// load a single file | ||
loadjs('/path/to/foo.js', function() { | ||
// foo.js loaded | ||
}); | ||
1. Load a single file | ||
```javascript | ||
loadjs('/path/to/foo.js', { | ||
success: function() { /* foo.js loaded */} | ||
}); | ||
``` | ||
// load multiple files (in parallel) | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], function() { | ||
// foo.js & bar.js loaded | ||
}); | ||
2. Fetch files in parallel and load asynchronously | ||
// load multiple files (in series) | ||
loadjs('/path/to/foo.js', function() { | ||
loadjs('/path/to/bar.js', function() { | ||
// foo.js loaded then bar.js loaded | ||
```javascript | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], { | ||
success: function() { /* foo.js & bar.js loaded */ } | ||
}); | ||
}); | ||
``` | ||
3. Fetch files in parallel and load in series | ||
// add a bundle id | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', function() { | ||
// foo.js & bar.js loaded | ||
}); | ||
```javascript | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], { | ||
success: function() { /* foo.js and bar.js loaded in series */ }, | ||
async: false | ||
}); | ||
``` | ||
4. Add a bundle id | ||
// add a failure callback | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], | ||
'foobar', | ||
function() { /* foo.js & bar.js loaded */ }, | ||
function(pathsNotFound) { /* at least one path didn't load */ }); | ||
```javascript | ||
// add a bundle id | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { | ||
success: function() { /* foo.js & bar.js loaded */ } | ||
}); | ||
``` | ||
5. Add a failure callback | ||
// execute a callback after bundle finishes loading | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar'); | ||
```javascript | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', { | ||
success: function() { /* foo.js & bar.js loaded */ }, | ||
fail: function(pathsNotFound) { /* at least one path didn't load */ } | ||
}); | ||
``` | ||
loadjs.ready('foobar', function() { | ||
// foo.js & bar.js loaded | ||
}); | ||
6. Execute a callback after bundle finishes loading | ||
```javascript | ||
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar'); | ||
// .ready() can be chained together | ||
loadjs('/path/to/foo.js', 'foo'); | ||
loadjs('/path/to/bar.js', 'bar'); | ||
loadjs | ||
.ready('foo', function() { | ||
// foo.js loaded | ||
}) | ||
.ready('bar', function() { | ||
// bar.js loaded | ||
loadjs.ready('foobar', { | ||
success: function() { /* foo.js & bar.js loaded */ } | ||
}); | ||
``` | ||
7. Chain .ready() together | ||
// compose more complex dependency lists | ||
loadjs('/path/to/foo.js', 'foo'); | ||
loadjs('/path/to/bar.js', 'bar'); | ||
loadjs(['/path/to/thunkor.js', '/path/to/thunky.js'], 'thunk'); | ||
```javascript | ||
loadjs('/path/to/foo.js', 'foo'); | ||
loadjs('/path/to/bar.js', 'bar'); | ||
loadjs | ||
.ready('foo', { | ||
success: function() { /* foo.js loaded */ } | ||
}) | ||
.ready('bar', { | ||
success: function() { /* bar.js loaded */ } | ||
}); | ||
``` | ||
// wait for multiple depdendencies | ||
loadjs.ready(['foo', 'bar', 'thunk'], | ||
function() { | ||
// foo.js & bar.js & thunkor.js & thunky.js loaded | ||
}, | ||
function(depsNotFound) { | ||
if (depsNotFound.indexOf('foo') > -1) {}; // foo failed | ||
if (depsNotFound.indexOf('bar') > -1) {}; // bar failed | ||
if (depsNotFound.indexOf('thunk') > -1) {}; // thunk failed | ||
}); | ||
8. Compose more complex dependency lists | ||
```javascript | ||
loadjs('/path/to/foo.js', 'foo'); | ||
loadjs('/path/to/bar.js', 'bar'); | ||
loadjs(['/path/to/thunkor.js', '/path/to/thunky.js'], 'thunk'); | ||
// use .done() for more control | ||
loadjs.ready('my-awesome-plugin', function() { | ||
myAwesomePlugin(); | ||
}); | ||
// wait for multiple depdendencies | ||
loadjs.ready(['foo', 'bar', 'thunk'], { | ||
success: function() { | ||
// foo.js & bar.js & thunkor.js & thunky.js loaded | ||
}, | ||
fail: function(depsNotFound) { | ||
if (depsNotFound.indexOf('foo') > -1) {}; // foo failed | ||
if (depsNotFound.indexOf('bar') > -1) {}; // bar failed | ||
if (depsNotFound.indexOf('thunk') > -1) {}; // thunk failed | ||
} | ||
}); | ||
``` | ||
9. Use .done() for more control | ||
loadjs.ready('jquery', function() { | ||
// plugin requires jquery | ||
window.myAwesomePlugin = function() { | ||
if (!window.jQuery) throw "jQuery is missing!"; | ||
}; | ||
```javascript | ||
loadjs.ready('my-awesome-plugin', { | ||
success: function() { | ||
myAwesomePlugin(); | ||
} | ||
}); | ||
// plugin is done loading | ||
loadjs.done('my-awesome-plugin'); | ||
}); | ||
``` | ||
loadjs.ready('jquery', { | ||
success: function() { | ||
// plugin requires jquery | ||
window.myAwesomePlugin = function() { | ||
if (!window.jQuery) throw "jQuery is missing!"; | ||
}; | ||
// plugin is done loading | ||
loadjs.done('my-awesome-plugin'); | ||
} | ||
}); | ||
``` | ||
## Directory structure | ||
@@ -162,0 +189,0 @@ |
@@ -82,3 +82,3 @@ /** | ||
*/ | ||
function loadScript(path, callbackFn) { | ||
function loadScript(path, callbackFn, async) { | ||
var doc = document, | ||
@@ -88,2 +88,3 @@ s = doc.createElement('script'); | ||
s.src = path; | ||
s.async = (async === undefined) ? true : async; | ||
@@ -105,7 +106,7 @@ s.onload = s.onerror = s.onbeforeload = function(ev) { | ||
*/ | ||
function loadScripts(paths, callbackFn) { | ||
function loadScripts(paths, callbackFn, async) { | ||
// listify paths | ||
paths = paths.push ? paths : [paths]; | ||
var i = paths.length, numWaiting = i, pathsNotFound = [], fn; | ||
var numWaiting = paths.length, x = numWaiting, pathsNotFound = [], fn, i; | ||
@@ -129,3 +130,3 @@ // define callback function | ||
// load scripts | ||
while (i--) loadScript(paths[i], fn); | ||
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async); | ||
} | ||
@@ -141,12 +142,11 @@ | ||
*/ | ||
function loadjs(paths, arg1, arg2, arg3) { | ||
var bundleId, successFn, failFn; | ||
function loadjs(paths, arg1, arg2) { | ||
var bundleId, args; | ||
// bundleId | ||
if (arg1 && !arg1.call) bundleId = arg1; | ||
// bundleId (if string) | ||
if (arg1 && arg1.trim) bundleId = arg1; | ||
// args (default is {}) | ||
args = (bundleId ? arg2 : arg1) || {}; | ||
// successFn, failFn | ||
successFn = bundleId ? arg2 : arg1; | ||
failFn = bundleId ? arg3 : arg2; | ||
// throw error if bundle is already defined | ||
@@ -163,8 +163,9 @@ if (bundleId) { | ||
loadScripts(paths, function(pathsNotFound) { | ||
if (pathsNotFound.length) (failFn || devnull)(pathsNotFound); | ||
else (successFn || devnull)(); | ||
// success and fail callbacks | ||
if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound); | ||
else (args.success || devnull)(); | ||
// publish bundle load event | ||
publish(bundleId, pathsNotFound); | ||
}); | ||
}, args.async); | ||
} | ||
@@ -176,11 +177,10 @@ | ||
* @param {(string|string[])} deps - List of bundle ids | ||
* @param {Function} [successFn] - Success callback | ||
* @param {Function} [failFn] - Fail callback | ||
* @param {Object} args - success/fail arguments | ||
*/ | ||
loadjs.ready = function (deps, successFn, failFn) { | ||
loadjs.ready = function (deps, args) { | ||
// subscribe to bundle load event | ||
subscribe(deps, function(depsNotFound) { | ||
// execute callbacks | ||
if (depsNotFound.length) (failFn || devnull)(depsNotFound); | ||
else (successFn || devnull)(); | ||
if (depsNotFound.length) (args.fail || devnull)(depsNotFound); | ||
else (args.success || devnull)(); | ||
}); | ||
@@ -187,0 +187,0 @@ |
@@ -83,3 +83,3 @@ loadjs = (function () { | ||
*/ | ||
function loadScript(path, callbackFn) { | ||
function loadScript(path, callbackFn, async) { | ||
var doc = document, | ||
@@ -89,2 +89,3 @@ s = doc.createElement('script'); | ||
s.src = path; | ||
s.async = (async === undefined) ? true : async; | ||
@@ -106,7 +107,7 @@ s.onload = s.onerror = s.onbeforeload = function(ev) { | ||
*/ | ||
function loadScripts(paths, callbackFn) { | ||
function loadScripts(paths, callbackFn, async) { | ||
// listify paths | ||
paths = paths.push ? paths : [paths]; | ||
var i = paths.length, numWaiting = i, pathsNotFound = [], fn; | ||
var numWaiting = paths.length, x = numWaiting, pathsNotFound = [], fn, i; | ||
@@ -130,3 +131,3 @@ // define callback function | ||
// load scripts | ||
while (i--) loadScript(paths[i], fn); | ||
for (i=0; i <= x - 1; i++) loadScript(paths[i], fn, async); | ||
} | ||
@@ -142,12 +143,11 @@ | ||
*/ | ||
function loadjs(paths, arg1, arg2, arg3) { | ||
var bundleId, successFn, failFn; | ||
function loadjs(paths, arg1, arg2) { | ||
var bundleId, args; | ||
// bundleId | ||
if (arg1 && !arg1.call) bundleId = arg1; | ||
// bundleId (if string) | ||
if (arg1 && arg1.trim) bundleId = arg1; | ||
// args (default is {}) | ||
args = (bundleId ? arg2 : arg1) || {}; | ||
// successFn, failFn | ||
successFn = bundleId ? arg2 : arg1; | ||
failFn = bundleId ? arg3 : arg2; | ||
// throw error if bundle is already defined | ||
@@ -164,8 +164,9 @@ if (bundleId) { | ||
loadScripts(paths, function(pathsNotFound) { | ||
if (pathsNotFound.length) (failFn || devnull)(pathsNotFound); | ||
else (successFn || devnull)(); | ||
// success and fail callbacks | ||
if (pathsNotFound.length) (args.fail || devnull)(pathsNotFound); | ||
else (args.success || devnull)(); | ||
// publish bundle load event | ||
publish(bundleId, pathsNotFound); | ||
}); | ||
}, args.async); | ||
} | ||
@@ -177,11 +178,10 @@ | ||
* @param {(string|string[])} deps - List of bundle ids | ||
* @param {Function} [successFn] - Success callback | ||
* @param {Function} [failFn] - Fail callback | ||
* @param {Object} args - success/fail arguments | ||
*/ | ||
loadjs.ready = function (deps, successFn, failFn) { | ||
loadjs.ready = function (deps, args) { | ||
// subscribe to bundle load event | ||
subscribe(deps, function(depsNotFound) { | ||
// execute callbacks | ||
if (depsNotFound.length) (failFn || devnull)(depsNotFound); | ||
else (successFn || devnull)(); | ||
if (depsNotFound.length) (args.fail || devnull)(depsNotFound); | ||
else (args.success || devnull)(); | ||
}); | ||
@@ -188,0 +188,0 @@ |
@@ -1,1 +0,1 @@ | ||
loadjs=function(){function n(n,e){n=n.push?n:[n];var t,o,r,f,u=[],a=n.length,l=a;for(t=function(n,t){t.length&&u.push(n),l--,l||e(u)};a--;)o=n[a],r=i[o],r?t(o,r):(f=c[o]=c[o]||[],f.push(t))}function e(n,e){if(n){var t=c[n];if(i[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e){var t=document,o=t.createElement("script");o.src=n,o.onload=o.onerror=o.onbeforeload=function(t){e(n,t.type[0],t.defaultPrevented)},t.head.appendChild(o)}function o(n,e){n=n.push?n:[n];var o,r=n.length,f=r,u=[];for(o=function(n,t,o){if("e"==t&&u.push(n),"b"==t){if(!o)return;u.push(n)}f--,f||e(u)};r--;)t(n[r],o)}function r(n,t,r,i){var c,a,l;if(t&&!t.call&&(c=t),a=c?r:t,l=c?i:r,c){if(c in u)throw new Error("LoadJS: Bundle already defined");u[c]=!0}o(n,function(n){n.length?(l||f)(n):(a||f)(),e(c,n)})}var f=function(){},u={},i={},c={};return r.ready=function(e,t,o){return n(e,function(n){n.length?(o||f)(n):(t||f)()}),r},r.done=function(n){e(n,[])},r}(); | ||
loadjs=function(){function n(n,e){n=n.push?n:[n];var t,o,r,i,f=[],a=n.length,s=a;for(t=function(n,t){t.length&&f.push(n),s--,s||e(f)};a--;)o=n[a],r=u[o],r?t(o,r):(i=c[o]=c[o]||[],i.push(t))}function e(n,e){if(n){var t=c[n];if(u[n]=e,t)for(;t.length;)t[0](n,e),t.splice(0,1)}}function t(n,e,t){var o=document,r=o.createElement("script");r.src=n,r.async=void 0===t?!0:t,r.onload=r.onerror=r.onbeforeload=function(t){e(n,t.type[0],t.defaultPrevented)},o.head.appendChild(r)}function o(n,e,o){n=n.push?n:[n];var r,i,f=n.length,u=f,c=[];for(r=function(n,t,o){if("e"==t&&c.push(n),"b"==t){if(!o)return;c.push(n)}f--,f||e(c)},i=0;u-1>=i;i++)t(n[i],r,o)}function r(n,t,r){var u,c;if(t&&t.trim&&(u=t),c=(u?r:t)||{},u){if(u in f)throw new Error("LoadJS: Bundle already defined");f[u]=!0}o(n,function(n){n.length?(c.fail||i)(n):(c.success||i)(),e(u,n)},c.async)}var i=function(){},f={},u={},c={};return r.ready=function(e,t){return n(e,function(n){n.length?(t.fail||i)(n):(t.success||i)()}),r},r.done=function(n){e(n,[])},r}(); |
@@ -21,5 +21,7 @@ /** | ||
it('should call success callback on valid path', function(done) { | ||
loadjs(['assets/file1.js'], function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
done(); | ||
loadjs(['assets/file1.js'], { | ||
success: function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
done(); | ||
} | ||
}); | ||
@@ -30,11 +32,12 @@ }); | ||
it('should call fail callback on invalid path', function(done) { | ||
loadjs(['assets/file-doesntexist.js'], | ||
function() { | ||
throw "Executed success callback"; | ||
}, | ||
function(pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js'); | ||
done(); | ||
}); | ||
loadjs(['assets/file-doesntexist.js'], { | ||
success: function() { | ||
throw "Executed success callback"; | ||
}, | ||
fail: function(pathsNotFound) { | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
@@ -44,6 +47,8 @@ | ||
it('should call success callback on two valid paths', function(done) { | ||
loadjs(['assets/file1.js', 'assets/file2.js'], function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
loadjs(['assets/file1.js', 'assets/file2.js'], { | ||
success: function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
} | ||
}); | ||
@@ -54,12 +59,13 @@ }); | ||
it('should call fail callback on one invalid path', function(done) { | ||
loadjs(['assets/file1.js', 'assets/file-doesntexist.js'], | ||
function() { | ||
throw "Executed success callback"; | ||
}, | ||
function(pathsNotFound) { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js'); | ||
done(); | ||
}); | ||
loadjs(['assets/file1.js', 'assets/file-doesntexist.js'], { | ||
success: function() { | ||
throw "Executed success callback"; | ||
}, | ||
fail: function(pathsNotFound) { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsNotFound.length, 1); | ||
assert.equal(pathsNotFound[0], 'assets/file-doesntexist.js'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
@@ -82,6 +88,8 @@ | ||
it('should create a bundle id and a callback inline', function(done) { | ||
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle4', function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
loadjs(['assets/file1.js', 'assets/file2.js'], 'bundle4', { | ||
success: function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
} | ||
}); | ||
@@ -101,9 +109,12 @@ }); | ||
loadjs | ||
.ready('bundle5', function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
bothDone(); | ||
}) | ||
.ready('bundle6', function() { | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
bothDone(); | ||
.ready('bundle5', { | ||
success: function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
bothDone(); | ||
}}) | ||
.ready('bundle6', { | ||
success: function() { | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
bothDone(); | ||
} | ||
}); | ||
@@ -117,6 +128,8 @@ }); | ||
loadjs.ready(['bundle7', 'bundle8'], function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
loadjs.ready(['bundle7', 'bundle8'], { | ||
success: function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
} | ||
}); | ||
@@ -130,12 +143,13 @@ }); | ||
loadjs.ready(['bundle9', 'bundle10'], | ||
function() { | ||
throw "Executed success callback"; | ||
}, | ||
function(depsNotFound) { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(depsNotFound.length, 1); | ||
assert.equal(depsNotFound[0], 'bundle10'); | ||
done(); | ||
}); | ||
loadjs.ready(['bundle9', 'bundle10'], { | ||
success: function() { | ||
throw "Executed success callback"; | ||
}, | ||
fail: function(depsNotFound) { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(depsNotFound.length, 1); | ||
assert.equal(depsNotFound[0], 'bundle10'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
@@ -146,4 +160,6 @@ | ||
// add handler | ||
loadjs.ready('plugin1', function() { | ||
done(); | ||
loadjs.ready('plugin1', { | ||
success: function() { | ||
done(); | ||
} | ||
}); | ||
@@ -161,4 +177,6 @@ | ||
// add handler | ||
loadjs.ready('plugin2', function() { | ||
done(); | ||
loadjs.ready('plugin2', { | ||
success: function() { | ||
done(); | ||
} | ||
}); | ||
@@ -174,6 +192,8 @@ }); | ||
setTimeout(function() { | ||
loadjs.ready('bundle1', function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
loadjs.ready('bundle1', { | ||
success: function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
} | ||
}); | ||
@@ -186,6 +206,8 @@ }, 1000); | ||
// define callback | ||
loadjs.ready('bundle2', function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
loadjs.ready('bundle2', { | ||
success: function() { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
assert.equal(pathsLoaded['file2.js'], true); | ||
done(); | ||
} | ||
}); | ||
@@ -199,2 +221,43 @@ | ||
it('should support async false', function(done) { | ||
var numCompleted = 0, | ||
numTests = 20, | ||
paths = ['assets/asyncfalse1.js', 'assets/asyncfalse2.js']; | ||
// run tests sequentially | ||
var testFn = function(paths) { | ||
loadjs(paths, { | ||
success: function() { | ||
var f1 = paths[0].replace('assets/', ''); | ||
var f2 = paths[1].replace('assets/', ''); | ||
// check load order | ||
assert.isTrue(pathsLoaded[f1]); | ||
assert.isFalse(pathsLoaded[f2]); | ||
// increment tests | ||
numCompleted += 1; | ||
if (numCompleted === numTests) { | ||
// exit | ||
done(); | ||
} else { | ||
// reset register | ||
pathsLoaded = {}; | ||
// run test again | ||
paths.reverse(); | ||
testFn(paths); | ||
} | ||
}, | ||
async: false | ||
}); | ||
} | ||
// run tests | ||
testFn(paths); | ||
}); | ||
// Un-'x' this for testing ad blocked scripts. | ||
@@ -208,7 +271,7 @@ // Ghostery: Disallow "Google Adservices" | ||
loadjs([blockedScript, 'assets/file1.js'], | ||
function() { | ||
loadjs([blockedScript, 'assets/file1.js'], { | ||
success: function() { | ||
throw "Executed success callback"; | ||
}, | ||
function(pathsNotFound) { | ||
fail: function(pathsNotFound) { | ||
assert.equal(pathsLoaded['file1.js'], true); | ||
@@ -218,4 +281,5 @@ assert.equal(pathsNotFound.length, 1); | ||
done(); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
336445
37
11493
253