Skip to content

Instantly share code, notes, and snippets.

@phoboslab
Last active Apr 10, 2017
Embed
What would you like to do?
ig.module(
'plugins.json'
)
.defines(function(){ "use strict";
ig.JSON = ig.Class.extend({
data: null,
path: '',
staticInstantiate: function( path ) {
return ig.JSON.cache[path] || null;
},
init: function( path ) {
this.path = path;
this.load();
},
load: function( loadCallback ) {
if( this.loaded ) {
if( loadCallback ) {
loadCallback( this.path, true );
}
return;
}
else if( !this.loaded && ig.ready ) {
this.loadCallback = loadCallback || null;
var that = this;
var req = new XMLHttpRequest();
if( req.overrideMimeType ) {
req.overrideMimeType("application/json");
}
req.onreadystatechange = function() {
if( req.readyState == req.DONE && req.status == 200 ) {
that.onload(req);
}
else if( req.readyStat == req.DONE && req.status != 200 ) {
that.onerror(req);
}
};
req.open('GET', ig.prefix + this.path + ig.nocache);
req.send();
}
else {
ig.addResource( this );
}
ig.JSON.cache[this.path] = this;
},
onload: function( req ) {
this.data = JSON.parse(req.responseText);
this.loaded = true;
if( this.loadCallback ) {
this.loadCallback( this.path, true );
}
},
onerror: function( req ) {
this.failed = true;
if( this.loadCallback ) {
this.loadCallback( this.path, false );
}
}
});
ig.JSON.cache = {};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment