ChildOfCode


Code, Maker, Robotic, Open Source. Knowledge Bases


JavaScript CallBack

ComponentOne.js

function ComponentOneClass(parent)  
{
    this.Parent = parent;
    var thisClass = this;

    //Properties
    this.responseCallback = null;

};


ComponentOneClass.prototype.ProcessAction = function(theValue)  
{
    if(theValue){
        this.responseCallback("Callback return");
    }else{
         Ti.API.log("Get Error");
    };

};

ComponentOneClass.prototype.CallOtherMethod = function(theValue,theCallback)  
{
    this.responseCallback = theCallback;
    theProcess = this.ProcessAction(theValue);
};

//finally, export the module
module.exports = ComponentOneClass;  

ComponentTwo.js

function ComponentTwoClass(parent)  
{
    var thisClass = this;
    this.Parent = parent;

    //CallBack
    this.Action_AfterGetCallBack = function(theReturn)
    {
        Ti.API.log("Return === "+ theReturn);       
    };


    //methods
    this.CallBackFunction();

};


ComponentTwoClass.prototype.CallBackFunction = function()  
{
    var thisClass = this;

    thisClass.Parent.ComponentOneClass.CallOtherMethod(true,this.Action_AfterGetCallBack);

};


//finally, export the module
module.exports = ComponentTwoClass;  

theMainApp.js

function theMainAppClass(app)  
{
    var thisClass = this;

    this.App = app;
    this.OSName = Ti.Platform.osname.toLowerCase();
    this.OSVersion = Ti.Platform.version;
    this.ScreenWidth = (this.OSName == "android") ? Ti.Platform.displayCaps.platformWidth / Ti.Platform.displayCaps.logicalDensityFactor : Ti.Platform.displayCaps.platformWidth;
    this.ScreenHeight = (this.OSName == "android") ? Ti.Platform.displayCaps.platformHeight / Ti.Platform.displayCaps.logicalDensityFactor : Ti.Platform.displayCaps.platformHeight;

    //Component One
    var ComponentOne = this.InitScript('ComponentOne');
    this.ComponentOneClass = new ComponentOne(this);

     //Component Two
    var ComponentTwo = this.InitScript('ComponentTwo');
    this.ComponentTwoClass = new ComponentTwo(this);

}

theMainAppClass.prototype.InitScript = function(theName)  
{
    switch(theName)
    {
        case "ComponentOne" :
            return require("Components/ComponentOne");
            break;

        case "ComponentTwo" :
            return require("Components/ComponentTwo");
            break;    

    }
};


//make sure this is the last line of the script
module.exports = theMainAppClass;

app.js

//bootstrap and check dependencies
if (Ti.version < 1.8)  
{
    alert('Sorry - this application template requires Titanium Mobile SDK 1.8 or later');
}

//Native Module 
var calculatorModule = require('com.cogs.calculator');

// This is a single context application with multiple windows in a stack
var theMainApp = require('theMainApp');  
var theMainAppClass = new theMainApp(this);