Search code examples
javascriptmoduletitaniumappcelerator-mobilecommonjs

Passing events through modules in Titanium SDK


How can i pass events through module hierarchy?

I have the main app.js, there i load the module welcome.js (with require("welcome") ) as a window and inside welcome i load modules home.js (the view containing the login and register buttons) and login.js. The problem is i don't know what is the right method to notify app.js and welcome.js about events in login.js. For example when the user wants to go back, inside the welcome i have to hide login and show home, when the user logs in i have to destroy the welcome and load main.js inside app.js.

Should i create a callback in app.js and pass it to welcome which passes it to login, create a global event listener in app and fire an event in login, or is there a better way i haven't found yet?


Solution

  • The easiest way to do it is to use application level event

    in login.js fire an application level event :

    Ti.App.fireEvent('yourEventName',{
                                       msg:'this is an event from login'
                                      });
    

    and in other file (welcome.js or app.js) listen to your event :

    Ti.App.addEventListener('yourEventName',function(e){
                                      alert(e.msg);
      });