preload.io-json
v1.1.0
Published
Preload.io loader for json files
Downloads
8
Readme
preload.io-json
Preload.io loader for json
Installation
npm i preload.io-json
Polyfills
Image requires a few polyfills to work everywhere, to give some flexibility they are not included by default.
npm i -S whatwg-fetch regenerator
import 'regenerator/runtime'
import 'whatwg-fetch'
Regenerator
is currently a requirement for the async stuff, but a version is included with babel-polyfill
so if you’re using that then you’re good to go. Use whichever version of fetch
you like, if necessary.
There will be a fairly obvious console error logged if these are omitted.
Getting Started
Install preload.io and register the json loader with it
import Preloader from 'preload.io'
import { EVENTS } from 'preload.io'
import JSONLoader from 'preload.io-json'
let preloader = new Preloader()
preloader.register( new JSONLoader() )
Then just load the resource and listen for the response, which will be parsed already
let id = preloader.load( 'https://api.github.com/users/mattstyles' )
preloader.on( EVENTS.LOAD, event => {
if ( event.id === id ) {
console.log( event.res ) // { login: 'mattstyles' ... }
}
})
Passing options to fetch
JSONLoader
uses fetch
to preload the resource, options you supply will be passed to fetch
, in this order of precedence:
preloader.load({
resource: 'stuff.json',
options: {
mode: 'no-cors'
}
})
const jsonLoader = new JSONLoader({
mode: 'no-cors'
})
const preloader = new Preloader({
mode: 'no-cors'
})