resource-loader
Advanced tools
Comparing version 1.1.4 to 1.2.0
{ | ||
"name": "resource-loader", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"main": "./src/index.js", | ||
@@ -5,0 +5,0 @@ "description": "A generic asset loader, made with web games in mind.", |
@@ -135,4 +135,41 @@ var async = require('async'), | ||
* | ||
* This function can take a wide variety of different parameters. The only thing that is always | ||
* required the url to load. All the following will work: | ||
* | ||
* ```js | ||
* loader | ||
* // normal param syntax | ||
* .add('key', 'http://...', function () {}) | ||
* .add('http://...', function () {}) | ||
* .add('http://...') | ||
* | ||
* // object syntax | ||
* .add({ | ||
* name: 'key2', | ||
* url: 'http://...' | ||
* }, function () {}) | ||
* .add({ | ||
* url: 'http://...' | ||
* }, function () {}) | ||
* .add({ | ||
* name: 'key3', | ||
* url: 'http://...' | ||
* onComplete: function () {} | ||
* }) | ||
* .add({ | ||
* url: 'https://...', | ||
* onComplete: function () {}, | ||
* crossOrigin: true | ||
* }) | ||
* | ||
* // you can also pass an array of objects or urls or both | ||
* .add([ | ||
* { name: 'key4', url: 'http://...', onComplete: function () {} }, | ||
* { url: 'http://...', onComplete: function () {} }, | ||
* 'http://...' | ||
* ]); | ||
* ``` | ||
* | ||
* @alias enqueue | ||
* @param name {string} The name of the resource to load. | ||
* @param [name] {string} The name of the resource to load, if not passed the url is used. | ||
* @param url {string} The url for this resource, relative to the baseUrl of this loader. | ||
@@ -148,2 +185,32 @@ * @param [options] {object} The options for the load. | ||
Loader.prototype.add = Loader.prototype.enqueue = function (name, url, options, cb) { | ||
// special case of an array of objects or urls | ||
if (Array.isArray(name)) { | ||
for (var i = 0; i < name.length; ++i) { | ||
this.add(name[i]); | ||
} | ||
return this; | ||
} | ||
// if an object is passed instead of params | ||
if (typeof name === 'object') { | ||
cb = url || name.callback || name.onComplete; | ||
options = name; | ||
url = name.url; | ||
name = name.name || name.key || name.url; | ||
} | ||
// case where no name is passed shift all args over by one. | ||
if (typeof url !== 'string') { | ||
cb = options; | ||
options = url; | ||
url = name; | ||
} | ||
// now that we shifted make sure we have a proper url. | ||
if (typeof url !== 'string') { | ||
throw new Error('No url passed to add resource to loader.'); | ||
} | ||
// options are optional so people might pass a function and no options | ||
if (typeof options === 'function') { | ||
@@ -154,2 +221,3 @@ cb = options; | ||
// check if resource already exists. | ||
if (this.resources[name]) { | ||
@@ -156,0 +224,0 @@ throw new Error('Resource with name "' + name + '" already exists.'); |
49668
1110