Skip to content

Commit 1b1773f

Browse files
Gaspar Robertfecz0
authored andcommitted
add optional attributes param
1 parent 8b81309 commit 1b1773f

2 files changed

Lines changed: 35 additions & 22 deletions

File tree

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ David Walsh's [super simple loader](https://davidwalsh.name/javascript-loader) f
88
npm i -S @prepair/basic-loader
99
```
1010

11-
* Requires browser environment (dom).
12-
* Transpiled to es2015+ie context (polyfills not included).
11+
- Requires browser environment (dom).
12+
- Transpiled to es2015+ie context (polyfills not included).
1313

1414
## usage
1515

1616
```
1717
import load from '@prepair/basic-loader'
1818
1919
Promise.all([
20-
load.js('lib/highlighter.js'),
21-
load.js('lib/main.js'),
20+
load.js('lib/highlighter.js'),
21+
load.js('lib/main.js'),
2222
load.css('lib/highlighter.css'),
2323
load.img('images/logo.png')
2424
]).then(() => {
@@ -28,18 +28,27 @@ Promise.all([
2828
});
2929
```
3030

31+
## add optional attributes
32+
33+
```
34+
load.js('lib/main.js', {
35+
'data-foo': 'new-attribute',
36+
'data-bar': 'new-attribute-2'
37+
})
38+
```
39+
3140
## caveats
3241

33-
* Loading is not sequential of course. In the above example "main.js" may be parsed
42+
- Loading is not sequential of course. In the above example "main.js" may be parsed
3443
before "highlighter.js". If you want in order loading, use a sequential promise
3544
executor or a then chain.
36-
* Style loading may not work with very old mobile borwsers.
45+
- Style loading may not work with very old mobile borwsers.
3746
[Followup is here](https://github.com/w3core/import.js/issues/2), see addendum below.
38-
* The e2e test runner breaks (in phantom, but the test.html works in the browser)
47+
- The e2e test runner breaks (in phantom, but the test.html works in the browser)
3948
this is caused by an old phantomjs version in the _mocha-phantomjs_ package.
4049
[Issue is here](https://github.com/nathanboktae/mocha-phantomjs/issues/248). Solutions:
41-
* either wait for the upstream package to be updated
42-
* or update the binary manually (inside node_modules)
50+
- either wait for the upstream package to be updated
51+
- or update the binary manually (inside node_modules)
4352

4453
## exposed test
4554

src/basic-loader.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1-
function _load (tag) {
2-
return function (url) {
1+
function _load(tag) {
2+
// attributes example: { 'data-test': 'new-attribute-here' }
3+
return function(url, attributes = {}) {
34
// This promise will be used by Promise.all to determine success or failure
45
return new Promise((resolve, reject) => {
56
let element = document.createElement(tag);
6-
let parent = 'body';
7-
let attr = 'src';
7+
let parent = "body";
8+
let attr = "src";
89

910
// Important success and error for the promise
1011
element.onload = () => resolve(url);
1112
element.onerror = () => reject(url); // maybe we should remove the broken node, who knows
1213

1314
// Need to set different attributes depending on tag type
1415
switch (tag) {
15-
case 'script':
16+
case "script":
1617
element.async = true;
1718
break;
18-
case 'link':
19-
element.type = 'text/css';
20-
element.rel = 'stylesheet';
21-
attr = 'href';
22-
parent = 'head';
19+
case "link":
20+
element.type = "text/css";
21+
element.rel = "stylesheet";
22+
attr = "href";
23+
parent = "head";
2324
}
2425

2526
// Inject into document to kick off loading
2627
element[attr] = url;
28+
Object.keys(attributes).forEach(key => {
29+
element.setAttribute(key, attributes[key]);
30+
});
2731
document[parent].appendChild(element);
2832
});
2933
};
3034
}
3135

3236
// exporting a "default" would render the amd package to work differently
3337
module.exports = {
34-
css: _load('link'),
35-
js: _load('script'),
36-
img: _load('img')
38+
css: _load("link"),
39+
js: _load("script"),
40+
img: _load("img")
3741
};

0 commit comments

Comments
 (0)