Skip to content

Commit 66f8cbb

Browse files
committed
add support for before callback
1 parent e2fc9c9 commit 66f8cbb

File tree

9 files changed

+55
-2
lines changed

9 files changed

+55
-2
lines changed

AUTHORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ Authors ordered by first contribution.
33
Andres Morey <[email protected]>
44
revyh https://github.com/revyh
55
Ryan Burke https://github.com/codeburke
6+
Todd Williams https://github.com/toddw

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ Note: LoadJS treats empty CSS files as load failures in IE (to get around lack o
126126
});
127127
```
128128

129+
1. Execute a callback before script tags is embedded
130+
131+
```javascript
132+
loadjs(['/path/to/foo.js', '/path/to/bar.js'], {
133+
before: function(path, scriptEl) { /* called for each script node before embedded */ }
134+
});
135+
```
136+
129137
1. Execute a callback after bundle finishes loading
130138

131139
```javascript

dist/loadjs.js

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function loadFile(path, callbackFn, args, numTries) {
8484
var doc = document,
8585
async = args.async,
8686
maxTries = (args.numRetries || 0) + 1,
87+
beforeCallbackFn = (args.before || devnull),
8788
isCss,
8889
e;
8990

@@ -132,6 +133,9 @@ function loadFile(path, callbackFn, args, numTries) {
132133
// execute callback
133134
callbackFn(path, result, ev.defaultPrevented);
134135
};
136+
137+
// execute before callback
138+
beforeCallbackFn(path, e);
135139

136140
// add to document
137141
doc.head.appendChild(e);

dist/loadjs.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/loadjs.umd.js

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function loadFile(path, callbackFn, args, numTries) {
9292
var doc = document,
9393
async = args.async,
9494
maxTries = (args.numRetries || 0) + 1,
95+
beforeCallbackFn = (args.before || devnull),
9596
isCss,
9697
e;
9798

@@ -140,6 +141,9 @@ function loadFile(path, callbackFn, args, numTries) {
140141
// execute callback
141142
callbackFn(path, result, ev.defaultPrevented);
142143
};
144+
145+
// execute before callback
146+
beforeCallbackFn(path, e);
143147

144148
// add to document
145149
doc.head.appendChild(e);

src/loadjs.js

+4
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function loadFile(path, callbackFn, args, numTries) {
8383
var doc = document,
8484
async = args.async,
8585
maxTries = (args.numRetries || 0) + 1,
86+
beforeCallbackFn = (args.before || devnull),
8687
isCss,
8788
e;
8889

@@ -131,6 +132,9 @@ function loadFile(path, callbackFn, args, numTries) {
131132
// execute callback
132133
callbackFn(path, result, ev.defaultPrevented);
133134
};
135+
136+
// execute before callback
137+
beforeCallbackFn(path, e);
134138

135139
// add to document
136140
doc.head.appendChild(e);

test/assets/loadjs/loadjs.js

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function loadFile(path, callbackFn, args, numTries) {
8484
var doc = document,
8585
async = args.async,
8686
maxTries = (args.numRetries || 0) + 1,
87+
beforeCallbackFn = (args.before || devnull),
8788
isCss,
8889
e;
8990

@@ -132,6 +133,9 @@ function loadFile(path, callbackFn, args, numTries) {
132133
// execute callback
133134
callbackFn(path, result, ev.defaultPrevented);
134135
};
136+
137+
// execute before callback
138+
beforeCallbackFn(path, e);
135139

136140
// add to document
137141
doc.head.appendChild(e);

test/assets/loadjs/loadjs.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/tests.js

+28
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ describe('LoadJS tests', function() {
4747
}
4848
});
4949
});
50+
51+
52+
it('should call before callback before embedding into document', function(done) {
53+
var scriptTags = [];
54+
55+
loadjs(['assets/file1.js', 'assets/file2.js'], {
56+
before: function(path, el) {
57+
scriptTags.push({
58+
path: path,
59+
el: el
60+
});
61+
62+
// add cross origin script for file2
63+
if (path === 'assets/file2.js') {
64+
el.crossOrigin = 'anonymous';
65+
}
66+
},
67+
success: function () {
68+
assert.equal(scriptTags[0].path, 'assets/file1.js');
69+
assert.equal(scriptTags[1].path, 'assets/file2.js');
70+
71+
assert.equal(scriptTags[0].el.crossOrigin, undefined);
72+
assert.equal(scriptTags[1].el.crossOrigin, 'anonymous');
73+
74+
done();
75+
}
76+
});
77+
});
5078

5179

5280
it('should call success callback on two valid paths', function(done) {

0 commit comments

Comments
 (0)