Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[![Coverage Status](https://coveralls.io/repos/github/john-ko/dynamic-script-loader/badge.svg?branch=master)](https://coveralls.io/github/john-ko/dynamic-script-loader?branch=master)
[![Build Status](https://travis-ci.org/john-ko/dynamic-script-loader.svg?branch=master)](https://travis-ci.org/john-ko/dynamic-script-loader)


# Script Loader
A script loader for modular components in es6 (build process coming soon). This is built so that each components being responsible for what 3rd party dependencies they need.
- if script has not been loaded, it will load the script, run the one time setup callback, then execute the `then`
- if a script has already been loaded, it will execute the `then`
- if script has not been loaded, it will load the script, and return a promise
- `onLoadHandler` and `onErrorHandler` will execute on script load and on script error
- script promise will be resolved when the script is loaded, rejects if it errors
- if a script has already been requested to load throught dynamic script loader, it will return the existing promise
- all script proimses are added to `window.dynamicScriptLoader` global object

# Install
`npm i -S dynamic-script-loader`
Expand All @@ -14,21 +16,16 @@ A script loader for modular components in es6 (build process coming soon). This
simple

```
const ScriptLoader = require('dynamic-script-loader')
const script = new ScriptLoader()

script.load({
src: '//path/to/someExternalJS.js',
async: true, // default to true, you can leave this out
}), function oneTimeSetUp () {
// window.someExternalJS setup here
})
.then(function externalJSSuccessResolver () {
// this runs after one time setup
})
.catch(function scriptDidntLoad () {
console.log(':(')
})
const scriptLoader = require('dynamic-script-loader')

scriptLoader({
src: ''//path/to/someExternalJS.js'
},
onLoadHandler, // optional
onErrorHandler // optional
)
.then(resolverFunction)
.catch(catchFunction)
```

## Vue
Expand All @@ -38,12 +35,11 @@ heres a couple of ways you can use the script loader

### using it as a method
```
import DynamicScriptLoader from 'dynamic-script-loader'
import scriptLoader from 'dynamic-script-loader'

Vue.use({
install: function (Vue, options) {
Vue.prototype.$script = new DynamicScriptLoader()
}
Vue.prototype.$scriptLoader = scriptLoader
})

// inside a Vue component
Expand All @@ -56,7 +52,7 @@ data () {

mounted () {
// the loader will not load libraries if they have already been loaded
this.$script.load({
this.$scriptLoader({
src: '//some-cdn/src/js/library.js',
async: true
}, () => {
Expand Down
Loading