Created
September 10, 2012 18:46
-
-
Save pec1985/3692865 to your computer and use it in GitHub Desktop.
Global event problem
This file contains 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
function Window1() { | |
var win = Ti.UI.createWindow({ | |
fullscreen: false, | |
backgroundColor: '#ccc' | |
}); | |
var btn = Ti.UI.createButton({ | |
title: 'open next', | |
top: 50, | |
width: Ti.UI.SIZE, | |
height: Ti.UI.SIZE | |
}); | |
win.add(btn); | |
var num = 0; | |
btn.addEventListener('click', function(){ | |
num++; | |
var w = Window2(num); | |
w.open(); | |
}); | |
return win; | |
} | |
function Window2(num) { | |
var win = Ti.UI.createWindow({ | |
fullscreen: false, | |
backgroundColor: '#ccc', | |
title: 'Window: #' + num | |
}); | |
var label = Ti.UI.createLabel({ | |
top:10, | |
width: 200, | |
height: 50, | |
backgroundColor: 'white', | |
color: 'black', | |
text: '' | |
}); | |
var btn = Ti.UI.createButton({ | |
width: Ti.UI.SIZE, | |
height: Ti.UI.SIZE, | |
title: 'fire event' | |
}); | |
win.add(label); | |
win.add(btn); | |
btn.addEventListener('click', function(){ | |
Ti.App.fireEvent('change_label', {text: 'event fired'}); | |
}); | |
// Memory leak | |
Ti.App.addEventListener('change_label', function(e){ | |
label.text = e.text; | |
Ti.API.info('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-'); | |
Ti.API.info('-=-=-=-=-EVENT FIRED-=-=-=-=-=-'); | |
}); | |
return win; | |
} | |
Window1().open(); |
This file contains 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
function Window1() { | |
var win = Ti.UI.createWindow({ | |
fullscreen: false, | |
backgroundColor: '#ccc' | |
}); | |
var btn = Ti.UI.createButton({ | |
title: 'open next', | |
top: 50, | |
width: Ti.UI.SIZE, | |
height: Ti.UI.SIZE | |
}); | |
win.add(btn); | |
var num = 0; | |
btn.addEventListener('click', function(){ | |
num++; | |
var w = Window2(num); | |
w.open(); | |
}); | |
return win; | |
} | |
function Window2(num) { | |
var win = Ti.UI.createWindow({ | |
fullscreen: false, | |
backgroundColor: '#ccc', | |
title: 'Window: #' + num | |
}); | |
var label = Ti.UI.createLabel({ | |
top:10, | |
width: 200, | |
height: 50, | |
backgroundColor: 'white', | |
color: 'black', | |
text: '' | |
}); | |
var btn = Ti.UI.createButton({ | |
width: Ti.UI.SIZE, | |
height: Ti.UI.SIZE, | |
title: 'fire event' | |
}); | |
win.add(label); | |
win.add(btn); | |
btn.addEventListener('click', function(){ | |
Ti.App.fireEvent('change_label', {text: 'event fired'}); | |
}); | |
function changeLabel(e) { | |
label.text = e.text; | |
Ti.API.info('-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-'); | |
Ti.API.info('-=-=-=-=-EVENT FIRED-=-=-=-=-=-'); | |
} | |
// No memory leak | |
Ti.App.addEventListener('change_label', changeLabel); | |
win.addEventListener('close', function(){ | |
// Remove the event on window close | |
Ti.App.removeEventListener('change_label', changeLabel); | |
}); | |
return win; | |
} | |
Window1().open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment