Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

What is fireEvent? How do I use it?

Hi guys,

I do not understand how to use fireEvent aside from Titanium.App.fireEvent('someEvent'). The docs show fireEvent as a method in most (if not all) modules.

I tried to do this and I got an error. If fireEvent is called inside another event handler, it does not fire at all. Please help!

var win = Ti.UI.createWindow();
win.open();

var view = Ti.UI.createView(
{
    width: 100,
    height: 100,
    backgroundColor: '#FF0000'
});

win.add(view);

view.addEventListener('someEvent', function()
{
    Ti.API.info('someEvent');
});

view.addEventListener('click', function()
{
    Ti.API.info('click');    
});

view.fireEvent('someEvent');

[ERROR] Script Error = Result of expression 'view.fireEvent' [undefined] is not a function.

I'm running this in the iPhone emulator by the way. Many thanks!

Jon

— asked March 11th 2010 by First Name Last Name
  • fireevent
  • iphone
  • mobile
  • titanium mobile
  • view
0 Comments

9 Answers

  • Accepted Answer

    fireEvent is useful for sending data between js files and windows.
    you need to do something like this…
    to fire an event:

    Ti.App.fireEvent('foo', {name:'bar'});
    

    and to listen for that event and recieve the data:

    Ti.App.addEventListener('foo', function(data) 
    { 
         var name = data.name; 
    });
    

    var name will be equal to 'bar'.

    — answered March 11th 2010 by Steve Burtenshaw
    permalink
    2 Comments
    • I read this might be really bad for performance, is that right?

      — commented May 6th 2013 by Joris
    • This is an app-wide event. It has very specific uses. It's a terrible answer.

      No-one has even answered the question.

      How do you make a specific view/controller dispatch an event?

      — commented August 4th 2015 by Andrew Gallasch
  • Too bad I'm not at my Mac right now. My Win machine here at work ran it nicely in the Android emulator. I got the "someEvent" message in the Titanium Developer console.

    The only difference is that I used var win=Ti.UI.currentWindow() instead of creating and opening a new window.

    — answered March 11th 2010 by Bill Dawson
    permalink
    0 Comments
  • (Ignore the parens at end of currentWindow in my prev post.)

    — answered March 11th 2010 by Bill Dawson
    permalink
    0 Comments
  • @Steve: Yes I happen to know how to call fireEvent from the Ti.App module. And yes it's useful when sending messages between windows, webviews, etc. But the docs is actually saying a lot more.

    Under Ti.UI module, there are 3 common methods across all objects: addEventListener, removeEventListener and fireEvent. So does that mean I can call fireEvent in any view? UIView? TableView? ScrollView?

    @Bill: My code above is for the app.js file that is why I do not need to access Ti.UI.currentWindow. But following your suggestion still throws an error.

    Here's what I'm trying to do:

    • create a view as a container
    • create more views as children of the container above
    • depending on what happens inside the container, i want to manually dispatch a custom event from the container so a window-level listener can listen to that custom event

    I hope I made it clearer. Thanks Steve and Bill! :)

    — answered March 11th 2010 by First Name Last Name
    permalink
    0 Comments
  • Sorry. My unordered list did not show as bullets. This formatting sucks.

    — answered March 11th 2010 by First Name Last Name
    permalink
    0 Comments
  • So I'm at my Mac now, and the code runs without error (I pasted exactly your code into an empty app.js). So no error, BUT it also doesn't fire the event.

    Apparently this is a known issue, with a ticket opened 2 days ago.

    The problem seems to be with custom events on views – exactly what you want to do. :( Hopefully they'll get to it soon. If you create an account at lighthouseapp, you can "watch" the ticket.

    — answered March 11th 2010 by Bill Dawson
    permalink
    0 Comments
  • Bill, glad you found the ticket to this issue. It seems like it works in Android but not in iPhone. I'll should start watching that ticket.

    Cheers!

    — answered March 11th 2010 by First Name Last Name
    permalink
    0 Comments
  • Bill, glad you found the ticket to this issue. It seems like it works in Android but not in iPhone. I'll should start watching that ticket.

    Cheers!

    — answered March 11th 2010 by First Name Last Name
    permalink
    0 Comments
  • Si it possible to draw an Overlay in Map view in the latest Titanium version? if not are there any work around for this?

    — answered October 14th 2012 by Visuddha Karunaratne
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.