Skip to content

Instantly share code, notes, and snippets.

@toddpi314
Created June 8, 2012 15:23
Show Gist options
  • Save toddpi314/2896141 to your computer and use it in GitHub Desktop.
Save toddpi314/2896141 to your computer and use it in GitHub Desktop.
Rich_Domain_Components_Loader
Namespace.Register('DeepElement.ThreeOneFour.Helpers.ComponentLoader');
/*
* Asynchronous Loader for Components
*/
DeepElement.ThreeOneFour.Helpers.ComponentLoader = new Class({
instances: null,
initialize: function(instances) {
this.instances = instances;
},
unload: function(successCallback, failureCallback) {
if (successCallback != null) {
var workQueue = new Queue();
for (var i = 0; i <= this.instances.length - 1; i++) {
var instance = this.instances[i];
if (instance != null) workQueue.enqueue(instance);
}
this.callInstance('unload', workQueue, successCallback, failureCallback);
}
},
load: function(successCallback, failureCallback) {
if (successCallback != null) {
var workQueue = new Queue();
for (var i = 0; i <= this.instances.length - 1; i++) {
var instance = this.instances[i];
if (instance != null) workQueue.enqueue(instance);
}
this.callInstance('load', workQueue, successCallback, failureCallback);
}
},
callInstance: function(functionName, workQueue, successCallback, failureCallback) {
if (!workQueue.isEmpty() && successCallback != null) {
var instance = workQueue.dequeue();
var self = this;
instance[functionName](function() {
self.callInstance(functionName, workQueue, successCallback, failureCallback);
}, function() {
if (failureCallback != null) failureCallback();
});
} else {
successCallback();
}
},
getInstances: function() {
return this.instances;
}
});​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment