Skip to content

Instantly share code, notes, and snippets.

@Coder2012

Coder2012/promises

Created Feb 2, 2016
Embed
What would you like to do?
var $ = require('../js/lib/jQuery');
var THREE = require('../js/lib/three');
'use strict';
var loader = {
options: {
globalDeferred: $.Deferred(),
deps: [],
textures: [],
models: []
},
load: function(items) {
var name, type, path;
var self = loader;
items.forEach(function(item){
name = item.name;
type = item.type;
path = item.path;
if(item.type == 'texture'){
self.options.deps.push(self.loadTexture(name, path));
}else{
self.options.deps.push(self.loadModel(name, path));
}
});
return this.options.globalDeferred.promise();
},
loadTexture: function(name, path){
var deferred = $.Deferred();
var self = this;
var textureLoader = new THREE.TextureLoader();
textureLoader.load('../img/' + path, function(texture){
self.options.textures[name] = texture;
// deferred.resolve();
});
return deferred.promise();
},
loadModel: function(name, path){
var deferred = $.Deferred();
var self = this;
var jsonLoader = new THREE.JSONLoader();
jsonLoader.load('../model/' + path, function(geometry){
self.options.models[name] = geometry;
// deferred.resolve();
});
return deferred.promise();
},
getTexture: function(name){
return loader.options.textures[name];
},
getModel: function(name){
console.log("looking for: ", name);
return loader.options.models[name];
}
};
$.when.apply($, loader.options.deps).done(function(){
console.log("loaded: ");
loader.options.globalDeferred.resolve();
});
module.exports = loader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.