Lets say you have a couple object literals defined that are classes to various apps, like:
var dataStorage = { some class to store app data info }
var setup = { some class to setup the data in dataStorage }
var app = { some class for the main app view }
var controller = { some class to control the flow }
and the flow is dependent on callbacks. So the controller will make its own instances of the object literal class's defined using
var app_instance = Object.create(app)
and then start each app accordingly using .startApp(callback) for each object/class above, which takes a callback that is executed when that app or object/class is done. This typically means something was updated in dataStorage, which is also passed to each app or object/class when called. When each app or object/class is finished it calls the same method it is in inside of controller which then executes differently because dataStorage is updated. See the code it might help.
var controller = {
createStarterObjects: function() {
((this.app === null || typeof this.app === 'undefined') ? this.app = Object.create(App) : console.log('app obj instance exists'));
((this.setup === null || typeof this.setup === 'undefined') ? this.setup = Object.create(Setup) : console.log('setup obj instance exists'));
((this.data === null || typeof this.data === 'undefined') ? this.data = Object.create(AppStorage) : console.log('data obj instance exists'));
},
startApp: function() {
this.createStarterObjects();
var that = this;
this.data.startApp(function(response) {
if(!response) {
that.setup.startApp(that.data, function(rsp) {
if(!rsp) {
that.setup.destroyApp();
alert('fatal setup error');
} else {
that.setup.destroyApp();
that.startApp();
}
});
} else {
that.app.startApp(that.data, function(rsp) {
if(!rsp) {
that.app.destroyApp();
alert('fatal app error');
} else {
console.log('success');
}
})
}
})
}
}
var controllerInstance = Object.create(controller);
controllerInstance.startApp();
This is a vague look at it, but basically what is the downfall in calling the same function inside itself after a callback is retued? Having the callbacks retued after the app is done spreads the execution out but I would guess that stack is building on each call. Please let me know if you know what is going on here and the pitfall and workarounds are that exist.
Thanks to all in advanced!
