Ti.App.fireEvent from inside a CommonJs module not fired globally

You must Login before you can answer or comment on any questions.

Hi. I decided on using the CommonJs approach for an app I'm building. The idea is completely decouple UI and Model by having the exchange data using events and event payloads. I've seen the tutorial from Aaron Saunders at the Developer blog, doing exactly that, so I went ahead. However, it is not working and it seems that the Titanium.App.fireEvent doesn't go beyond it's variable scope.

Below the code: app.js

var AppController=require('js/app/controller/AppController');
AppController.accountGet();
Ti.App.addEventListener('app:accountGet',function(){
    alert('event received');
});
Below the code in AppController.js
exports.accountGet=function(_callback){
    Ti.API.info('accoutnGet invoked');
    Ti.App.fireEvent('app:accountGet');
    if(_callback){
        _callback();
    }
};
Callbacks get executed as long as it is not Ti.App.fireEvent . Is this the way it's supposed to work or is it a bug? Thanks

1 Answer

Accepted Answer

I think the problem is that you are calling "accountGet" function in which event is fired, but you attach event listener after "accountGet" function. That means that you have fired event before you attached listener to it.

Try this in "app.js":

var AppController=require('js/app/controller/AppController');
 
Ti.App.addEventListener('app:accountGet',function(){
    alert('event received');
});
 
AppController.accountGet();

— answered 2 years ago by Ivan Škugor
answer permalink
2 Comments
  • Man, I am obviously tired :-), there's no way I would have noticed that today or tomorrow. Thanks a lot!

    — commented 2 years ago by Richard Lustemberg

  • NP. Trivial errors are sometimes the hardest to find. But second pair of eyes can notice them instantly. :)

    — commented 2 years ago by Ivan Škugor

Your Answer

Think you can help? Login to answer this question!