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
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'.
-
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. -
(Ignore the parens at end of currentWindow in my prev post.)
-
@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! :)
-
Sorry. My unordered list did not show as bullets. This formatting sucks.
-
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.
-
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!
-
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!
-
Si it possible to draw an Overlay in Map view in the latest Titanium version? if not are there any work around for this?