Created
December 19, 2010 10:41
-
-
Save omorandi/747260 to your computer and use it in GitHub Desktop.
Appcelerator Titanium Mobile - Custom events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var win1 = Titanium.UI.createWindow({ | |
title:'Window 1', | |
backgroundColor:'#fff' | |
}); | |
var label1 = Titanium.UI.createLabel({ | |
color:'#999', | |
text:'Window 1\nClick anywhere for opening Window 2', | |
textAlign:'center', | |
width:'auto' | |
}); | |
win1.add(label1); | |
win1.addEventListener('click', function(e) | |
{ | |
var win2 = Ti.UI.createWindow({title: 'win2', url: 'win2.js'}); | |
win2.open(); | |
}); | |
Ti.App.addEventListener('myEvent', function(e) | |
{ | |
Ti.API.info('my event fired!'); | |
Ti.API.info('Custom data sent with the event: ' + e.customData); | |
label1.text = 'I know you came from Window 2\ncustomData=' + e.customData; | |
}); | |
win1.open(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var win2 = Ti.UI.currentWindow; | |
win2.backgroundColor = 'black'; | |
var label = Titanium.UI.createLabel({ | |
color:'#999', | |
text:'I am Window 2\nclick anywhere for closing me', | |
textAlign:'center', | |
width:'auto' | |
}); | |
win2.add(label); | |
win2.addEventListener('click', function(e) | |
{ | |
var evt = {customData: 'myData'}; | |
Ti.App.fireEvent('myEvent', evt); | |
win2.close(); | |
}); |
That's actually a really clear explanation. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Showing how custom events work in Titanium Mobile.
In window1 we set a global event listener on the event 'myEvent'.
A click on window 1 creates and opens window 2.
A click on window 2 closes the window after firing a synthesized event 'myEvent' that gets catched by the event handler installed in window 1.
Events can carry data as well, so in window 2 we associate an object with the event, which we can check out in window 1.