Skip to content

Commit 7577f47

Browse files
committed
You can now pass in a JS object to the JSON Loader instead of a URL. It will use the object to populate the data with. This impacts any loader type that uses json, so atlas, tilemap, audio sprite, etc. Closes phaserjs#3147.
1 parent 76c7639 commit 7577f47

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/loader/File.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,24 @@ var File = new Class({
154154

155155
this.callback = callback;
156156

157-
this.src = GetURL(this, baseURL);
158-
159-
if (this.src.indexOf('data:') === 0)
157+
if (this.state === CONST.FILE_POPULATED)
160158
{
161-
console.log('Local data URI');
159+
this.onComplete();
160+
161+
callback(this);
162162
}
163163
else
164164
{
165-
this.xhrLoader = XHRLoader(this, globalXHR);
165+
this.src = GetURL(this, baseURL);
166+
167+
if (this.src.indexOf('data:') === 0)
168+
{
169+
console.log('Local data URI');
170+
}
171+
else
172+
{
173+
this.xhrLoader = XHRLoader(this, globalXHR);
174+
}
166175
}
167176
}
168177

src/loader/LoaderPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ var LoaderPlugin = new Class({
178178

179179
this.list.each(function (file)
180180
{
181-
if (file.state === CONST.FILE_PENDING && this.inflight.size < this.maxParallelDownloads)
181+
if (file.state === CONST.FILE_POPULATED || (file.state === CONST.FILE_PENDING && this.inflight.size < this.maxParallelDownloads))
182182
{
183183
this.inflight.set(file);
184184

src/loader/const.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ var FILE_CONST = {
3434
// File has been destroyed
3535
FILE_DESTROYED: 18,
3636

37+
// File was populated from local data and doesn't need an HTTP request
38+
FILE_POPULATED: 19,
39+
3740
TEXTURE_ATLAS_JSON_ARRAY: 20,
3841
TEXTURE_ATLAS_JSON_HASH: 21
3942

src/loader/filetypes/JSONFile.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var JSONFile = new Class({
1212

1313
initialize:
1414

15+
// url can either be a string, in which case it is treated like a proper url, or an object, in which case it is treated as a ready-made JS Object
16+
1517
function JSONFile (key, url, path, xhrSettings)
1618
{
1719
var fileKey = (typeof key === 'string') ? key : GetFastValue(key, 'key', '');
@@ -27,6 +29,14 @@ var JSONFile = new Class({
2729
};
2830

2931
File.call(this, fileConfig);
32+
33+
if (typeof fileConfig.url === 'object')
34+
{
35+
// Object provided instead of a URL, so no need to actually load it (populate data with value)
36+
this.data = fileConfig.url;
37+
38+
this.state = CONST.FILE_POPULATED;
39+
}
3040
},
3141

3242
onProcess: function (callback)

0 commit comments

Comments
 (0)